Change a volume drive letter with Powershell

I ran across a useful command that instantly reassigns a drive to another letter, and another one liner that detects a CD-ROM and assigns it a particular letter. For additional details and a richer script, check out this blog post from The Admin Guy. The piece that I found most useful from his script was (replace ! with the pipe symbol):

(gwmi Win32_cdromdrive).drive ! %{$a = mountvol $_ /l;mountvol $_ /d;$a = $a.Trim();mountvol x: $a}

That one liner detects a CD-ROM drive and re-assigns it the letter X. Works great on systems with one CD-ROM. Taking this as an example, I slightly modified the command to take as an input a fixed drive letter and then re-assign it another letter. Here the drive changes from X to Y. Replace ! with the pipe symbol.

echo x: ! %{$a = mountvol $_ /l;mountvol $_ /d;$a = $a.Trim();mountvol y: $a}

One great thing about these commands is that the change happens immediately, unlike a VBS script I was trying to use earlier this week that just hacked the registry and required a reboot. I’m using these scripts in my Server 2008 R2 ESX template to re-assign various drive letters during the sysprep process.

Print Friendly, PDF & Email

Related Posts

Notify of
Inline Feedbacks
View all comments