Windows Tip Sheet

Look Mom, No Scripting!

Keep track of drive use, but do it without taking the scripting route.

Keeping track of drive utilization is a never-ending administrative task. People often resort to complex VBScript with WMI and messy stuff like objects. Well, you can get the same results without writing a single line of code. I'll show you how to leverage WMI to get hard drive utilization without writing a single line of VBScript code.

If you have PowerShell, this a pretty easy task. Open a PowerShell session and run this command -- it's a long one and should be typed as a single line:

get-wmiobject -query "select deviceid,size,freespace from win32_logicaldisk where drivetype=3" |select deviceID,Size,FreeSpace | format-table –auto

You should get something that looks like this:

deviceID     Size    FreeSpace
-------- ----------- ---------
C:       15726702592 382550016
E:       24280993792 689537024
S:       24280993792 689537024

The sizes are in bytes. Hey, I never said it would be beautiful. I had to pipe the results through a few cmdlets to get rid of extra information that WMI returns.

If you want to check on drives on a remote computer simply add –computer SERVERNAME before –query.

If you want to report on multiple computers, the expression is a little more complicated. Servers.txt has a list of computer names:

PS C:\> $servers=get-content servers.txt
PS C:\> $d={get-wmiobject –computer $computer-query "select deviceid,size,freespace from win32_logicaldisk where drivetype=3" |select deviceID,Size,FreeSpace | format-table -auto}
PS C:\> foreach ($s in $servers) {$computer=$s;$computer;&$d}

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at [email protected]; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

I'm using a script block so that the last command isn't a knuckle buster to type. This is by no means foolproof and obviously there's much more you can do. I merely wanted to get you started.

By the way, in case you were wondering, drivetype 3 means fixed hard drives, which is usually all you care about anyway. If you have any issues with the PowerShell commands, post your question in the PowerShell forum at ScriptingAnswers.com.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular