Snippet #2 – Checking for free disk space with PowerShell

Inspired by Dan’s last post, I thought I would share some more PowerShell goodness. A very quick one to start with: How to check for free disk space with PowerShell.

As you would expect with PowerShell its amazingly simple. Here is the function:

Function GetFreeDiskSpace([string]$DriveLetter, [string]$Measurement)

{

$Drives=Get-WmiObject -Class Win32_LogicalDisk

foreach($Drive in $Drives)

{

If ($Drive.Name -eq $DriveLetter)

{

[int]$GLOBAL:Freespace=$Drive.FreeSpace/“1$Measurement

}

}

}

The function simply queries the WMI LogicalDisk class and then converts the fresspace to GB, MB or KB. For example, if you want to find out how much free disk space is available for the c:\ in Gigabytes, you would call the function as follows:

GetFreeDiskSpace “C:” “GB”

write-host “Freespace = $Freespace

That’s all for now 🙂

Advertisements

One thought on “Snippet #2 – Checking for free disk space with PowerShell

  1. whoah this weblog is excellent i love reading your posts. Keep up the good work!

    You realize, lots of persons are hunting around for
    this information, you could aid them greatly.

Speak your mind...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s