Beta 42

Research and Development

Menu

PowerShell - Locking Drive Content

This will not hide drive letters but prohibit access to drive content. You need administrative privileges to set this setting.

function Hide-DriveContent {
    param($DriveLetter)

    $key = @{
        Path = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer'
        Name = 'NoViewOnDrive'
    } 


    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 -type DWORD
    }
}

Use the function like so:

PS> Hide-DriveContent D,Z    # prohibits access to drives D: and Z:
PS> Hide-DriveContent        # removes all restrictions

Log off and on again, or kill explorer.exe for changes to take effect.