PoshCode Archive  Artifact [a920f88bfd]

Artifact a920f88bfdb615d24fbdb0cc6d2c16c8fd9fcfb44c886cc711d28bebd66e491f:

  • File unix-df-h-analog.ps1 — part of check-in [c4e8281673] at 2018-06-10 13:43:19 on branch trunk — Prints “human-readable” information for specified drive (user: greg zakharov size: 1831)

# encoding: ascii
# api: powershell
# title: unix df -h analog
# description: Prints “human-readable” information for specified drive
# version: 0.1
# type: function
# author: greg zakharov
# license: CC0
# function: Get-DriveFormatData
# x-poshcode-id: 4581
# x-archived: 2013-11-06T02:14:39
# x-published: 2013-11-05T08:12:00
#
#
function Get-DriveFormatData {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true,
               ValueFromPipeline=$true)]
    [ValidatePattern("[a-z]")]
    [Char]$DriveLetter
  )
  
  begin {
    function vol([Char]$dl) {
      try {
        $rk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\MountedDevices')
        $rk.GetValueNames() | % {$hash = @{}}{
          $hash[$_] = [String]$rk.GetValue($_) -replace ' ', ''
        }{
          ($hash.GetEnumerator() | ? {$_.Value -eq $hash[('\DosDevices\' + $dl + ':')]}) | % {
            if ($_.Name -notmatch 'Dos') {$_.Name}
          }
        }
      }
      finally {
        if ($rk -ne $null) {$rk.Close()}
      }
    }
  }
  process {
    [IO.DriveInfo]::GetDrives() | % {$arr = @()}{
      $str = "" | select Drive, FileSystem, Size, Used, Avail, Use, Mounted, Label
      if ($_.Name -eq ($DriveLetter + ':\') -and $_.IsReady) {
        $str.Drive = $_.Name
        $str.FileSystem = $_.DriveFormat
        $str.Size = ("{0}Gb" -f [Math]::Round($_.TotalSize / 1Gb))
        $str.Used = ("{0:F2}Gb" -f (($_.TotalSize - $_.TotalFreeSpace) / 1Gb))
        $str.Avail = ("{0:F2}Gb" -f ($_.TotalFreeSpace / 1Gb))
        $str.Use = ("{0:F2}%" -f (($_.TotalSize - $_.TotalFreeSpace) * 100 / $_.TotalSize))
        $str.Mounted = (vol $_.Name[0])
        $str.Label = $_.VolumeLabel
        $arr += $str
      }
    }
  }
  end {
    $arr | ft -a
  }
}