Share via


Percentage in PowerShell - Disk Space Report

A quick way to make a Disk Report using PowerShell. There are many scripts available in Script Center. You can download one for your needs.

A teammate asked: How to add '%' in my disk report? Something to be appended in string or concatenating variables.

PowerShell Trick

$freespace = Get-WmiObject -Class Win32_logicalDisk | ? {$_.DriveType -eq '3'}$drive = ($FreeSpace.DeviceID).Split("=")"Your $drive Free Space Percentage is {0:P2}" -f ($freespace.FreeSpace / $freespace.Size)

The above code will do the trick.

Explanation:

{0:P2} is covered in Text and Regular Expression concepts. Indeed, it's PowerShell so you can do it many ways. Another trick is below:

$freespace = Get-WmiObject -Class Win32_logicalDisk | ? {$_.DriveType -eq '3'}$drive = ($FreeSpace.DeviceID).Split("=")#"Your $drive Free Space Percentage is {0:P2}" -f ($freespace.FreeSpace / $freespace.Size) [String]::Format("Your $Drive Free Space Percentage is {0:P2}" -f ($freespace.FreeSpace / $freespace.Size))

Enjoy PowerShell

Other Languages
This article is also available in the following languages: