PowerShell for Windows Admins

Aug 5 2011   11:58AM GMT

Setting the drive letter

Richard Siddaway Richard Siddaway Profile: 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.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: