Saturday, June 16, 2007

WMI Win32_Volume Class using PowerShell

I've been playing around this weekend with PowerShell and the Win32_Volume WMI class and have written some PowerShell scripts to exploit this class.

To get the volumes on a system is trivial:

$volumes = Get-WMIObject Win32_Volume

This returns an array of volume objects which you can iterate through and display. To display the whole collection, or to just see the first volume, you could do this:

$volumes
$volumes[0]

See here for a simple script that obtain the volumes on a system and then displays their information. Then look at this script to run a chkdsk on the system.

You can also use a method to determine how badly fragmented a volume is, and another to run a defrag pass on a volume. Look here for a script to display a defrag analysis of the C:\ and here to actually defrag the disk.

 

[Later Update]

The Win32_Volume WMI class is not available on all versions of Windows - I ran the scripts on a Longhorn B3 server.

 

Technorati tags: , ,

2 comments:

PReetamZ said...

PS C:\Server-Administration\Powershell> Get-WMIObject Win32_Volume
Get-WmiObject : Invalid class
At line:1 char:14
+ Get-WMIObject <<<< Win32_Volume

Do I need add-on snapsin for this

Unknown said...

According to MSDN:
http://msdn.microsoft.com/en-us/library/aa394515(VS.85).aspx

You need at least Server 03 for that, plus no Clients are supported (XP, Vista, 7)...