I’ve been working on a simple PowerShell command today to import into our endpoint management solution so we can alert on disks with low diskspace. It’s been a while since I’ve dabbled with PowerShell, and it reminded me just how flexible it is and much I love it!
So I thought I would walk you through the evolution of the command I ended up with.
Right, on with the tutorial.
First we’ll use the Cmdlet Get-Volume
This has displayed the volumes of the server and associated free diskspace, but the OCD in me needed them ordered neatly so then I ran Get-Volume | Sort-Object DriveLetter to order them by DriveLetter!
Now you can see the tables above shows a SizeRemaining field so we can use this to filter our results.
Type Get-Volume | Where-Object {$_.SizeRemaining -lt 10}
Now you can see our results only show F: as it is an empty CD-ROM, so 10 obviously doesn’t refer to GB. instead it refers to Bytes. So we’ll need to increase the size a little to detect any volumes with less than 10GB remaining.
Type Get-Volume | Where-Object {$_.SizeRemaining -lt 10000000000}
Right now we’re getting somewhere, we can see all volumes on the server with less than 10GB of free diskspace. But I’m not interested in the CD-ROM so let’s filter some more but this time using the DriveType field.
Type Get-Volume | Where-Object {($_.SizeRemaining -lt 10000000000) -and ($_.DriveType -eq “FIXED”)}
Now we’re not really interested in the System Reserved volume either so finally let’s filter this out of our results.
Type Get-Volume | Where-Object {($_.SizeRemaining -lt 10000000000) -and ($_.DriveType -eq “FIXED”) -and ($_.FileSystemLabel -ne “System Reserved”)}
Now one final tweak I’m going to do to make it more readable is convert the Bytes to GB. See the reference section at the end for a description of the math Bytes to GB conversion.
Get-Volume | Where-Object {([math]::truncate($_.SizeRemaining / 1GB) -lt 10)-and ($_.DriveType -eq “FIXED”) -and ($_.FileSystemLabel -ne “System Reserved”)}
And there you are, a simple one-liner to display volumes with less than 10GB of free diskspace remaining.
References:
PoweShell Tip of the week – Byte conversion https://technet.microsoft.com/en-us/library/ee692684.aspx
Where-Object https://technet.microsoft.com/en-us/library/ee177028.aspx
Get-Volume https://technet.microsoft.com/en-us/library/hh848646.aspx
PowerShell Variables: http://www.computerperformance.co.uk/powershell/powershell_variables.htm
Related Posts:
1. PowerShell: Get-ADComputer to retrieve computer last logon date – part 1
2. PowerShell: Get-ADUser to retrieve password last set and expiry information
3. Exchange PowerShell: How to list all SMTP email addresses in Exchange
4. Exchange PowerShell: How to enumerate Distribution Lists, managers and members
how would I go about setting it up so that if there is a drive with less than 10gb an email is sent out to a specific recipient. Thanks