I realize this question is pretty old, but I found it helpful but needed to go a different direction. If you have disks that don't have drive letters and are instead mounted as NTFS mount points or something like that, then the methods here won't find them. You do see them with the built-in PowerShell commands like Get-Volume
,Get-Partition
, and Get-Disk
but those don't support the -computername
param to get remote devices without using invoke-command and other methods. However, each of them is based on a ciminstance class in the "ROOT/Microsoft/Windows/Storage" namespace and you can get the same info using those classes
So I came up with this to find volumes on servers with low disk space (I actually have it in a function, but I'll just share the gist, you can remove the lines using the percent to just get all disks). Hopefully someone else finds it helpful
[string[]]$serverName = 'list','of','server','names' # you might use get-adcomputer with a filter to ou's with servers here
[decimal]$percentFreeThreshold = 9.99
$percent = $percentFreeThreshold/100;
$reports = New-Object -TypeName System.collections.generic.List['system.object']
$serverName | ForEach-Object {
$server = $_;
Write-Verbose "getting report for $server"
#get volumes with specified percent threshold or less free space
#Get the volumes with the ciminstance that get-volume uses
$report = Get-CimInstance -className "MSFT_Volume" -Namespace "ROOT/Microsoft/Windows/Storage" -ComputerName $server;
#the drive type returned seems to vary on how the command is run, it's either an id or a string, match either or for best results
$report = $report | Where-Object { ($_.Drivetype -eq 3) -or ($_.DriveType -eq 'Fixed') }
#get the volumes that are within the threshold
$report = $report | Where-Object {($_.SizeRemaining/$_.size) -le $percent}
#get the partitions of the server to fill the Partition value identifying the disk number and partition, the disk number helps identify what disk to expand in vctr
#we find the matching partition based on the size of the volume
# $partitions = Get-CimInstance -classname "win32_DiskPartition" -ComputerName $server;
$partitions = Get-CimInstance -className "MSFT_Partition" -Namespace "ROOT/Microsoft/Windows/Storage" -ComputerName $server;
# $Report = Get-CimInstance -className win32_logicaldisk -ComputerName $server -Filter "Drivetype=3" -ErrorAction SilentlyContinue | Where-Object {($_.freespace/$_.size) -le $percent}
#select the important pieces of the report and convert the number values to human-readable GigaBytes
if ($report) {
Write-Verbose "$server has low disk space, adding the report"
$view = $report | select-object -property `
@{N='server';E={$server}},
@{N='DriveLetter';E={if ($null -ne $_.DriveLetter) {$_.DriveLetter} else {'N/A'}}},
@{N='Friendly Name';E={$_.FileSystemLabel}},
@{N='Partition';E={
$part = ($partitions | Where-Object AccessPaths -Contains $_.path);
"Disk # $($part.DiskNumber) Partition # $($part.PartitionNumber)"
}},
@{N="Total Size (GB)";E={[math]::round($_.size/1GB,2)}},
@{N='FreeSpace (GB)';E={[math]::round($_.SizeRemaining/ 1GB,2)}},
@{N='% Free';E={[math]::round(($_.SizeRemaining/$_.size)*100,2)}}
$reports.add($view);
}
}
Example output
server : someFileServer
DriveLetter : N/A
Friendly Name : SomeDiskName
Partition : Disk # 2 Partition # 2
Total Size (GB) : 249.98
FreeSpace (GB) : 18.38
% Free : 7.35
You could also take that resulting report and turn it into an html table like this
$Body = "<html><body><div>$($report | ConvertTo-Html -Fragment)</br></br></div><div>Some Message describing the low disk space report table</div></body></html>"
and use a different PowerShell module like Mailozaurr to send the report in an email to your ticketing system or something like that.
I realize I didn't include all the disk information that exists in above examples. You could add that to the report the same was I added the partition info by finding the matching partition from the volume accesspath (as in the example) then taking the diskPath in the partition object and finding the matching diskpath in the MSFT_DISK
class ciminstance and that object would have serial number, make/model, partition style etc. I just don't utilize that info in my report, but you could easily add it with the patterns shown here.