Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Percent Free Space

Posted on March 15, 2006August 5, 2009

I've put together a function that will return the percentage free space on a given logical drive for a specified server. The function in the following script is really doing all the hard work. The rest of the script is simply a demonstration on how to call the function. The script uses WMI and queries the Win32_LogicalDisk for the size and freespace properties. With these values it's pretty simple to calculate a percentage.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

'GetPercentFreeDrive.vbs

'specify alternate credentials for remote systems
strUsername=""
strPassword=""

strSrv=InputBox("What computer do you want to query","Get Percent Free","localhost")

strDeviceID=InputBox("What drive do you want to query?","Get Percent Free","c:")

WScript.Echo UCase(strDeviceID) & " on " & UCase(strSrv) & " has " & GetPercent(strSrv,strDeviceID,strUsername,strPassword) & " free space."

Function GetPercent(strSrv,strDeviceID,strUsername,strPassword)
'deviceID is the drive letter and colon, eg E: with no trailing \

On Error Resume Next
Dim SWBemlocator,objWMI,objRef
Const wbemFlagReturnImmediately=&h10
Const wbemFlagForwardOnly=&h20

strQuery="Select DeviceID,Size,FreeSpace from Win32_LogicalDisk WHERE DeviceID='" & strDeviceID & "'"

'strip out any trailing \ if found
If Right(strDeviceID,1)="\" Then strDeviceID=Replace(strDeviceID,"\","")

'validate strDeviceID
If Len(strDeviceID)=2 And Right(strDeviceID,1)=":" Then

Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
SWBemlocator.Security_.ImpersonationLevel=3
SWBemlocator.Security_.AuthenticationLevel=WbemAuthenticationLevelPktPrivacy
Set objWMI=SWBemlocator.ConnectServer(strSrv,"\root\CIMV2",strUsername,strPassword)

Set objRef=objWMI.ExecQuery(strQuery,"WQL",wbemForwardOnly+wbemFlagReturnImmediately)

For Each dev In objRef
iFreeSpace=dev.FreeSpace
iSize=dev.Size
Next

If iSize="" Then

GetPercent="Drive Not Found"
Else
GetPercent=FormatPercent(iFreeSpace/iSize,2)
End If

Else
GetPercent="ERROR"

End If

End Function

Technorati Tags:
WMI
Scripting
VBScript


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d