PowerShell - Disks/Storage
Get-Disk
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
Get-CimInstance Win32_DiskPartition
Get-CimInstance Win32_LogicalDiskToPartition
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
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
Invoke-Command -ComputerName myRemoteComputer -ScriptBlock {Get-PSDrive -PSProvider Filesystem} | Format-Table -autosize
Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName myRemoteComputer
Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName myRemoteComputer -Filter DriveType=3 | Select-Object 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
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