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
Get-CimInstance Win32_LogicalDisk
Partitions
Get-CimInstance Win32_DiskPartition
Get-CimInstance Win32_LogicalDiskToPartition
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
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
Remote
Also works locally if you omit the -ComputerName optionInvoke-Command -ComputerName myRemoteComputer -ScriptBlock {Get-PSDrive -PSProvider Filesystem} | Format-Table -autosize
Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName myRemoteComputer
Get-CimInstance -Query "Select * from Win32_logicaldisk" -ComputerName myRemoteComputer
Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName myRemoteComputer -Filter DriveType=3 | Select-Object DeviceID, FreeSpace, Size
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, VolumeName, @{'Name'='Size (GB)'; 'Expression'={[math]::truncate($_.size / 1GB)}}, @{'Name'='Freespace (GB)'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}
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 computerTo 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
Get-CimInstance/Get-WmiObjecthttps://learn.microsoft.com/en-us/answers/questions/679803/how-powershell-7-64-bits-single-script-instancehttps://www.anyviewer.com/how-to/check-disk-space-remotely-powershell-2578.htmlhttps://learn.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.2https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/07-working-with-wmi?view=powershell-7.2https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/computer-system-hardware-classeshttps://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-provider