PS-Storage

PowerShell - Disks/Storage

Disks

Get-Disk

Drives

Get-PSDrive

Show just filesystems...

Get-PSDrive -PSProvider FileSystem

Map a network drive...

New-PSDrive -Name "H" -Root "\\SQL01\SQLBackup" -PSProvider FileSystem -Scope Local -Persist:$true

Map a network drive (Powershell 5+)...

New-SmbMapping -LocalPath H -RemotePath \\SQL01\SQLBackup -Persistent:$true

Get-CimInstance Win32_DiskDrive

DeviceID           Caption                              Partitions Size        Model--------           -------                              ---------- ----        -----\\.\PHYSICALDRIVE3 VMware Virtual disk SCSI Disk Device 1          21467980800 VMware Virtual disk SCSI Disk Device\\.\PHYSICALDRIVE2 VMware Virtual disk SCSI Disk Device 1          21467980800 VMware Virtual disk SCSI Disk Device\\.\PHYSICALDRIVE5 VMware Virtual disk SCSI Disk Device 1          53686402560 VMware Virtual disk SCSI Disk Device\\.\PHYSICALDRIVE1 VMware Virtual disk SCSI Disk Device 1          10733990400 VMware Virtual disk SCSI Disk Device\\.\PHYSICALDRIVE4 VMware Virtual disk SCSI Disk Device 1          53686402560 VMware Virtual disk SCSI Disk Device\\.\PHYSICALDRIVE0 VMware Virtual disk SCSI Disk Device 3          85896599040 VMware Virtual disk SCSI Disk Device

Get-CimInstance Win32_LogicalDisk

DeviceID DriveType ProviderName VolumeName     Size        FreeSpace-------- --------- ------------ ----------     ----        ---------A:       2C:       3                                     85304799232 30939459584D:       3                      Admin          10701762560 8977321984E:       3                      MS SQL Data    53550772224 51342426112F:       3                      MS SQL Logs    21338517504 19521474560G:       3                      MS SQL Backups 53550772224 51357671424T:       3                      MS SQL Tempdb  21338517504 19481001984Z:       5

Partitions

Get-CimInstance Win32_DiskPartition

Name             NumberOfBlocks       BootPartition        PrimaryPartition     Size                Index----             --------------       -------------        ----------------     ----                -----Disk #3, Part... 41676800             False                True                 21338521600         0Disk #2, Part... 41676800             False                True                 21338521600         0Disk #5, Part... 104591360            False                True                 53550776320         0Disk #1, Part... 20901888             False                True                 10701766656         0Disk #4, Part... 104591360            False                True                 53550776320         0Disk #0, Part... 921600               False                False                471859200           0Disk #0, Part... 202752               True                 True                 103809024           1Disk #0, Part... 166610944            False                True                 85304803328         2

Get-CimInstance Win32_LogicalDiskToPartition

Antecedent      : Win32_DiskPartition (DeviceID = "Disk #0, Partition #2")Dependent       : Win32_LogicalDisk (DeviceID = "C:")EndingAddress   : 85898297343StartingAddress : 593494016PSComputerName  :
Antecedent      : Win32_DiskPartition (DeviceID = "Disk #1, Partition #0")Dependent       : Win32_LogicalDisk (DeviceID = "D:")EndingAddress   : 10736369663StartingAddress : 34603008PSComputerName  :
Antecedent      : Win32_DiskPartition (DeviceID = "Disk #4, Partition #0")Dependent       : Win32_LogicalDisk (DeviceID = "E:")EndingAddress   : 53686042623StartingAddress : 135266304PSComputerName  :
Antecedent      : Win32_DiskPartition (DeviceID = "Disk #2, Partition #0")Dependent       : Win32_LogicalDisk (DeviceID = "F:")EndingAddress   : 21473787903StartingAddress : 135266304PSComputerName  :
Antecedent      : Win32_DiskPartition (DeviceID = "Disk #5, Partition #0")Dependent       : Win32_LogicalDisk (DeviceID = "G:")EndingAddress   : 53686042623StartingAddress : 135266304PSComputerName  :
Antecedent      : Win32_DiskPartition (DeviceID = "Disk #3, Partition #0")Dependent       : Win32_LogicalDisk (DeviceID = "T:")EndingAddress   : 21473787903StartingAddress : 135266304PSComputerName  :

Filesystem Usage

Filesystem usage reporting

(something like UNIX 'du')

function directory-summary($dir=".") { 

  get-childitem $dir | 

    % { $f = $_ ; 

        get-childitem -r $_.FullName | 

           measure-object -property length -sum | 

             select @{Name="Name";Expression={$f}},Sum}

}


directory-summary

directory-summary | sort sum

An alternate approach...

gci . | 

  %{$f=$_; gci -r $_.FullName | 

    measure-object -property length -sum |

    select  @{Name="Name"; Expression={$f}}, 

            @{Name="Sum (MB)"; 

            Expression={"{0:N3}" -f ($_.sum / 1MB) }}, Sum } |

  sort Sum -desc |

  format-table -Property Name,"Sum (MB)", Sum -autosize

Another approach...

function Get-DiskUsage ([string]$path=".") {

    $groupedList = Get-ChildItem -Recurse -File $path |

       Group-Object directoryName | 

          select name,@{name='length'; expression={($_.group |

             Measure-Object -sum length).sum } }

    foreach ($dn in $groupedList) {

        New-Object psobject -Property @{ directoryName=$dn.name; length=($groupedList | 

            where { $_.name -like "$($dn.name)*" } | Measure-Object -Sum length).sum }

    }

}


Get-DiskUage

Another approach...

function du($path=".") {

    Get-ChildItem $path |

    ForEach-Object {

        $file = $_

        Get-ChildItem -File -Recurse $_.FullName | Measure-Object -Property length -Sum |

        Select-Object -Property @{Name="Name";Expression={$file}},

                                @{Name="Size(MB)";Expression={[math]::round(($_.Sum / 1MB),2)}} # round 2 decimal places

    }

}


du | Sort-Object -Property "Size(MB)" -Descending

Filesystem capacity reporting

(something like UNIX 'df')

Local

Get-PSDrive -PSProvider FileSystem

Name           Used (GB)     Free (GB) Provider      Root                                              CurrentLocation----           ---------     --------- --------      ----                                              ---------------C                 145.51         91.31 FileSystem    C:\                                              Windows\System32Temp              145.51         91.31 FileSystem    C:\Users\myUser\AppData\Local\Te…

Get-PSDrive -PSProvider FileSystem | Format-Table Name, @{Name="Disk Size(GB)";Expression={"{0,8:N0}" -f($_.free/1gb +$_.used/1gb)}}, @{Name="Free (%)";Expression={"{0,6:P0}" -f($_.free / ($_.free +$_.used))}} ` -AutoSize

Name Disk Size(GB) Free (%)---- ------------- --------C         237         39%Temp      237         39%

Remote

Also works locally if you omit the -ComputerName option

Invoke-Command -ComputerName myRemoteComputer -ScriptBlock {Get-PSDrive -PSProvider Filesystem} | Format-Table -autosize

Name Used (GB) Free (GB) Provider Root CurrentLocation PSComputerName---- --------- --------- -------- ---- --------------- --------------C        62.74     12.16          C:\  Users\myuser\Documents MYREMOTECOMPUTERD         5.16      4.84          D:\                         MYREMOTECOMPUTERE        37.91     32.08          E:\                         MYREMOTECOMPUTERF       109.90    160.10          F:\                         MYREMOTECOMPUTERG       745.02    278.98          G:\                         MYREMOTECOMPUTERH       594.42    429.57          H:\                         MYREMOTECOMPUTERI                                 I:\                         MYREMOTECOMPUTERP         3.71     16.28          P:\                         MYREMOTECOMPUTER

Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName myRemoteComputer

DeviceID DriveType ProviderName VolumeName     Size          FreeSpace    PSComputerName-------- --------- ------------ ----------     ----          ---------    --------------C:       3                      OS             80422629376   13302902784  MYREMOTECOMPUTERD:       3                      Admin          10734268416   5194932224   MYREMOTECOMPUTERE:       3                      Application    75158777856   34426695680  MYREMOTECOMPUTERF:       3                      SQL            289907142656  171902038016 MYREMOTECOMPUTERG:       3                      Data           1099508477952 299621326848 MYREMOTECOMPUTERH:       3                      ApplicationDta 1099508477952 461250621440 MYREMOTECOMPUTERI:       5                                                                MYREMOTECOMPUTERP:       3                      Pagefile       21471686656   17483804672  MYREMOTECOMPUTER
This alternate command format produces the same output...
Get-CimInstance -Query "Select * from Win32_logicaldisk" -ComputerName myRemoteComputer

Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName myRemoteComputer -Filter DriveType=3 | Select-Object DeviceID, FreeSpace, Size

DeviceID    FreeSpace          Size--------    ---------          ----C:        13300502528   80422629376D:         5194932224   10734268416E:        34458365952   75158777856F:       171902038016  289907142656G:       299621330944 1099508477952H:       461250621440 1099508477952P:        17483804672   21471686656
This alternate command format produces the same output...
Get-CimInstance -Query "Select DeviceID, FreeSpace, Size from Win32_logicaldisk where drivetype = 3" -ComputerName myRemoteComputer | Format-Table @("DeviceID","FreeSpace","Size")

Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName myRemoteComputer -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[math]::truncate($_.size / 1GB)}}, @{'Name'='Freespace (GB)'; 'Expression'={[math]::truncate($_.freespace / 1GB)}} 

DeviceID Size (GB) Freespace (GB)-------- --------- --------------C:              74             12D:               9              4E:              69             32F:             269            160G:            1023            279H:            1023            429P:              19             16

To see all available retrievable properties use...

Get-CimInstance -ClassName Win32_LogicalDisk | Get-Member

Remote (Different Domain)

Assumes the remote server is reachable from your local computer

To use Get-CimInstance to connect to a remote computer in a different domain you can do something like this...

$CimSession = New-CimSession -ComputerName myRemoteComputer.myOtherDomain -Credential (Get-Credential)

You will be prompted for credentials. Enter username in the format: myOtherDomain\myOtherDomainUserUse -CimSession $CimSession in place of -ComputerName myRemoteComputer in any of the Get-CimInstance examples above....

Get-CimInstance -ClassName Win32_LogicalDisk -CimSession $CimSession

To use Invoke-Command to connect to a remote computer in a different domain you can do something like this...

Invoke-Command -ComputerName myRemoteComputer.myOtherDomain -Credential (Get-Credential) -ScriptBlock {Get-PSDrive -PSProvider Filesystem} | Format-Table -autosize

Bibliography