Checking Disk Space with PowerShell

This is another one of the code examples that I did in my PowerShell for Data Professionals session last week.  If you give it your computer name and then run it you will get back a stream or properties like I mentioned during the session. 

get-wmiobject -query `
"Select DeviceID,Size,Freespace from Win32_logicaldisk where drivetype=3" `
-computer "YourComputerName"

Now this runs fine but it brings back quite a bit more information than we really need.  I mean we specified columns in our WQL statement and it brought back the kitchen sink almost as if we had given it a SELECT *.  Now I don’t know why this happens I just know that it does (I have emailed a few people but haven’t heard back yet).  To get around this problemfeature all you have to do is take the output from the command you executed and pipe it to the Select-Object cmdlet and specify the columns that you want again.

get-wmiobject -query `
"Select DeviceID,Size,Freespace from Win32_logicaldisk where drivetype=3" `
-computer "YourComputerName" | select-object DeviceID, Size, FreeSpace

And voilà you have now taken something from the pipeline and done something with it that hopefully any SQL person in the world can understand.  So what’s next?  Well just change -computer again to the name of your favorite Dev server and see how much disk space you have.

*Don’t forget the disclaimer about running other people’s scripts!

Here’s the link to the msdn article that will have more info on those properties and the methods that you can use against

http://msdn.microsoft.com/en-us/library/aa394173(VS.85).aspx

Please Share This:

You may also like:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow:

Subcribe to Blog Via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

What I'm Saying on Twitter

Subscribe via feedburner

%d bloggers like this: