0

I made this script to display the values of disk spaces of multiple remote computers, but i need to add a filter to display only drives that are below 2% disks-space and to add the machine name to the table

Get-WmiObject Win32_LogicalDisk -ComputerName computer1, computer2, computer3 -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, @{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}}

1 Answer 1

2

I'd suggest using the newer Get-CimInstance cmdlet instead of Get-WmiObject.

You can add a Where-Object clause to filter on the percentage available and add the computername like this:

Get-CimInstance win32_LogicalDisk -ComputerName computer1, computer2, computer3 -Filter 'DriveType=3' | 
Where-Object { (($_.FreeSpace / $_.Size) * 100) -lt 2 } |
Select-Object SystemName, Caption, 
              @{Name ='Size (GB)'; Expression = {[string]::Format('{0:N0}',[math]::Truncate($_.Size / 1GB))}}, 
              @{Name ='Freespace (GB)'; Expression = {[string]::Format('{0:N0}',[math]::Truncate($_.FreeSpace / 1GB))}},
              @{Name ='pct used'; Expression = {"{0:N2}" -f (($_.FreeSpace / $_.Size) * 100)}} | 
Format-Table -AutoSize

The questions title says to filter on 5%, but the question text talks about 2% diskspace available

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.