2008-6
23

Here’s a simple little df (disk free from Unix) function for PowerShell:

function df ( $Path ) {
    if ( !$Path ) { $Path = (Get-Location -PSProvider FileSystem).ProviderPath }
    $Drive = (Get-Item $Path).Root -replace "\\"
    $Output = Get-WmiObject -Query "select freespace from win32_logicaldisk where deviceid = `'$drive`'"
    Write-Output "$($Output.FreeSpace / 1mb) MB"
}

(download script)

: http://halr9000.com/article/506

2008-06-23 16:08:30

Works nicely as is. I initially thought it needed a default path value for C:\ but I see now that it is taking the path of the current filesystem drive. This can be confusing. For example, change to a shared network drive. Then change to HKLM:. Run the function. You’ll get the value for the network drive. Obviously not a bug just something a user of the function should keep in mind.

I also changed my version to write just the value in bytes so that I can use the value any way I want.

2008-06-23 17:17:22

Yeah I was going for the easiest way to discard network drives and PSDrives. You are right that it may not be what a user is expecting. Thanks for the comments, always nice to have you drop by. :)

  • Microblog

  • Recent Posts

  • Recent Comments

  • meta

  • PowerShell Blogroll