It's a two-step process in PowerShell.
- Remove-SMBMapping
- Remove-PSDrive
It looks like the original link (https://blogs.msmvps.com/russel/tag/remove-psdrive/) for a bit of code is no longer producing the expected result (i.e., doesn't work). Here's another link to a problem that was posted on GitHub that had the PowerShell code: https://github.com/PowerShell/PowerShell/issues/7829
BE CAREFUL! The code will remove ALL mapped drives! It doesn't take any parameters!
I'll repost that code below:
<#
.SYNOPSIS
Unmaps network drives
.DESCRIPTION
Unmapdrives removes all currently mapped network drives. It's smart enough to
remove drives mapped with "net use", "New-SmbMapping" and "New-PSDrive". This
cmdlet accepts no parameters and assumes -Force for all unmappings.
.EXAMPLE
UnMapDrives
Unmaps all currently mapped network drives
.NOTES
Author: Charlie Russel
Copyright: 2015 by Charlie Russel
: Permission to use is granted but attribution is appreciated
Initial: 06/27/2015 (cpr)
ModHist:
:
#>
[CmdletBinding()]
# Build a dynamic list of currently mapped drives
$DriveList = Get-WMIObject Win32_LogicalDisk `
| Where-Object { $_.DriveType -eq 4 }
# Don't bother running this if we don't have any mapped drives
if ($DriveList) {
$SmbDriveList = $DriveList.DeviceID
} else {
Write-Host "No mapped drives found"
Return
}
Write-host "Unmapping drive: " -NoNewLine
Write-Host $SmbDriveList
Write-Host " "
Foreach ($drive in $SmbDriveList) {
$psDrive = $drive -replace ":" #remove unwanted colon from PSDrive name
Remove-SmbMapping -LocalPath $Drive -Force -UpdateProfile
If ( (Get-PSDrive -Name $psDrive) 2>$Null ) {
Remove-PSDrive -Name $psDrive -Force
}
}
Write-Host " "
# Report back all FileSystem drives to confirm that only local drives are present.
Get-PSDrive -PSProvider FileSystem`
It wouldn't take much to make this into a function that does take a parameter (maybe a single drive letter, or a list of drive letters, to remove). Until then, use this at your own risk!!!