PowerShell v2 Win32 Volume Methods Defrag

This morning I saw some chatter about using WMI to get defrag information.  Curious about the method I found this page on MSDN outlining the various methods on the Win32_Volume class:
Win32_Volume Methods 
And, more specifically, I found this page for the Defrag method itself:
Defrag method of the Win32_Volume Class
When I looked at the arguments I only saw one parameter that took input: Force.
uint32 Defrag(
[in]    boolean Force,
[out]   object DefragAnalysis
);
The Force parameter required a Boolean ($true or $false) to indicate whether defragmenting of the volume would occur even if free space is low.  The default is false.  To use this on a specific volume use this syntax:
Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Volume -Filter "DriveLetter=C:").Defrag($true) }
Now, the Job cmdlet is not necessary, but, I would rather do this to prevent issue with single-threading (and locking up) my shell.

Related Posts by Categories

0 comments:

Post a Comment