Aug 5 2011 11:58AM GMT
Posted by: Richard Siddaway
Disks
Setting the drive letter
Posted by: Richard Siddaway
I’ve been working on some WMI functions for disks recently. First off setting the disk drive letter.
function set-driveletter { [CmdletBinding()] param ( [string]$computer=".", [parameter(Mandatory=$true)] [string] [ValidatePattern("^[A-Z]{1}:{1}`$")] $olddrive, [parameter(Mandatory=$true)] [string] [ValidatePattern("^[A-Z]{1}:{1}`$")] $newdrive ) Get-WmiObject -Class Win32_Volume -ComputerName $computer -Filter "DriveLetter='$olddrive'" | Set-WmiInstance -Arguments @{DriveLetter=$newdrive} }
The difficult bit was getting the regular expression correct. It tests that we have a single letter and a colon. The old and new drives are mandatory parameters
Simple call to Get-WmiObject and pipe to Set-WmiInstance.
Use it like this:
set-driveletter -olddrive z: -newdrive i:
Jobs a good un.




