Jun 082012
 

Hiding Drive Letters with PowerShell

Sometimes you may want to hide drive letters in Windows Explorer from users. There’s a Registry key that can do this for you. It takes a bit mask where each drive has a bit. When the bit is set, the drive is hidden. Fortunately, in a previous tip you learned how to turn drive letters into bit masks, so here’s the function:

functionHide-Drive {
param($DriveLetter)

$key= @{
Path=HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
Name=‘NoDrives’
}

if ($DriveLetter-eq$null) {
Remove-ItemProperty @key
}
else {
$mask= 0
$DriveLetter|
ForEach-Object { $_.toUpper()[0] }
|
Sort-Object|
ForEach-Object { $mask+= [Math]::Pow(2,(([Byte]$_) 65)) }

Set-ItemProperty @key-Value$mask-typeDWORD
}

}

For example, to hide drives A, B, E, and Z, you would use it like this:

PS> Hide-DriveA,B,E,Z

To display all drives, omit arguments:

PS> Hide-Drive

Note that you need to have administrative privileges to change policies, and that policies may be overridden by group policy settings set up by your corporate IT.

For the changes to take effect, you need to log off and on again or kill your explorer.exe and restart it.

Leave a Reply