|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Remove a persistent mapping of a folder. |
| 4 | +
|
| 5 | +.PARAMETER DriveLetter |
| 6 | +The mapped drive letter to remove. |
| 7 | +
|
| 8 | +.PARAMETER Force |
| 9 | +Force using SourceDriveLabel, provided DriverLetter is the last mapping |
| 10 | +to the source drive. |
| 11 | +
|
| 12 | +.PARAMETER Reboot |
| 13 | +If true then reboot system; default is true. |
| 14 | +
|
| 15 | +.PARAMETER SourceDriveLabel |
| 16 | +New label to apply to the source drive; default it to retain the current label. |
| 17 | +This is only applied if DriveLetter is the last mapping to the source drive. |
| 18 | +#> |
| 19 | + |
| 20 | +using namespace System.IO |
| 21 | + |
| 22 | +[CmdletBinding(SupportsShouldProcess=$true)] |
| 23 | + |
| 24 | +param ( |
| 25 | + [Parameter(Mandatory=$true, Position=0, HelpMessage='Drive letter must be a mapped drive')] |
| 26 | + [ValidateLength(1, 1)] |
| 27 | + [ValidatePattern('[A-Z]')] |
| 28 | + [ValidateScript({ |
| 29 | + if ((Get-ItemPropertyValue "Registry::$DOSDevicesKey" -Name ('{0}:' -f $_) -ErrorAction 'SilentlyContinue') -eq $null) { |
| 30 | + Throw 'Drive letter not found or does not represent a mapped drive' |
| 31 | + } |
| 32 | + $true |
| 33 | + })] |
| 34 | + [string] $DriveLetter, |
| 35 | + |
| 36 | + [string] $SourceDriveLabel, |
| 37 | + |
| 38 | + [switch] $Reboot, |
| 39 | + [switch] $Force |
| 40 | + ) |
| 41 | + |
| 42 | +Begin |
| 43 | +{ |
| 44 | + $DriveIconsKey = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons' |
| 45 | + $DOSDevicesKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices' |
| 46 | + |
| 47 | + function UnmapDriveLetter () |
| 48 | + { |
| 49 | + # remove mapping |
| 50 | + if ($WhatIfPreference) { |
| 51 | + Write-Host "Remove-ItemProperty ""$DOSDevicesKey"" -Name ""${DriveLetter}:"" -Force" -ForegroundColor DarkYellow |
| 52 | + } else { |
| 53 | + Write-Verbose "Remove-ItemProperty ""$DOSDevicesKey"" -Name ""${DriveLetter}:"" -Force" |
| 54 | + Remove-ItemProperty "$DOSDevicesKey" -Name "${DriveLetter}:" -Force | Out-Null |
| 55 | + } |
| 56 | + Write-Verbose 'removed persistent mapping' |
| 57 | + |
| 58 | + # remove volume label for mapped drive |
| 59 | + if ($WhatIfPreference) { |
| 60 | + Write-Host "Remove-Item ""$DriveIconsKey\$DriveLetter"" -Recurse -Force" -ForegroundColor DarkYellow |
| 61 | + } else { |
| 62 | + Write-Verbose "Remove-Item ""$DriveIconsKey\$DriveLetter"" -Recurse -Force" |
| 63 | + if (Test-Path "$DriveIconsKey\$DriveLetter") { |
| 64 | + Remove-Item -Path "$DriveIconsKey\$DriveLetter" -Recurse -Force | Out-Null |
| 65 | + } |
| 66 | + } |
| 67 | + Write-Verbose 'removed mapped volume label' |
| 68 | + |
| 69 | + # remove volume label for source drive |
| 70 | + if ($WhatIfPreference) { |
| 71 | + Write-Host "Remove-Item ""$DriveIconsKey\$sourceLetter"" -Recurse -Force" -ForegroundColor DarkYellow |
| 72 | + } else { |
| 73 | + Write-Verbose "Remove-Item ""$DriveIconsKey\$sourceLetter"" -Recurse -Force" |
| 74 | + if (Test-Path "$DriveIconsKey\$sourceLetter") { |
| 75 | + Remove-Item "$DriveIconsKey\$sourceLetter" -Recurse -Force | Out-Null |
| 76 | + } |
| 77 | + } |
| 78 | + Write-Verbose 'removed source drive volume label' |
| 79 | + |
| 80 | + # restore volume label |
| 81 | + if ($WhatIfPreference) { |
| 82 | + Write-Host "set label of $sourceLetter drive to $label" -ForegroundColor DarkYellow |
| 83 | + } else { |
| 84 | + Write-Verbose "set label of $sourceLetter drive to $label" |
| 85 | + Set-Volume -DriveLetter $sourceLetter -NewFileSystemLabel $label |
| 86 | + } |
| 87 | + Write-Verbose 'set label of source drive' |
| 88 | + } |
| 89 | +} |
| 90 | +Process |
| 91 | +{ |
| 92 | + if (!$SourceDriveLabel -and (Test-Path "$DriveIconsKey\$DriveLetter")) { |
| 93 | + $SourceDriveLabel = (Get-ItemPropertyValue "$DriveIconsKey\$sourceLetter\DefaultLabel" -Name '(Default)' -ErrorAction 'SilentlyContinue') |
| 94 | + } |
| 95 | + |
| 96 | + if (!$SourceDriveLabel) { |
| 97 | + $SourceDriveLabel = if ($DriveLetter -eq ($env:HOMEDRIVE)[0]) { 'System' } else { 'Data' } |
| 98 | + } |
| 99 | + |
| 100 | + UnmapDriveLetter |
| 101 | + |
| 102 | + if ($Reboot -or !$PSBoundParameters.ContainsKey('Reboot')) |
| 103 | + { |
| 104 | + # must reboot in order for this to take effect |
| 105 | + if ($WhatIfPreference) { |
| 106 | + Write-Host 'Restart-Computer -Force -Timeout 0' -ForegroundColor DarkYellow |
| 107 | + } else { |
| 108 | + Restart-Computer -Force -Timeout 0 |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments