Unmapping Network Drives

Unmapping network drives with PowerShell should be easy, and it is, but with some caveats. If you always create your network drive mappings with New-PSDrive, then it's easy to unmap them with Remove-PSDrive. But if some of them are created with Group Policy, some of them with the legacy "net use" commands, some of them with New-SmbMapping, and some with New-PSDrive, it's really not that easy to fully clean up the list of mapped drives. I've used a variety of techniques over the years to map and unmap drives, and I've finally come to the conclusion that using New-PSDrive and Remove-PSDrive is the cleanest way for most things. However, if you've got a mix of mapped drives, created with more than one method, here's a script to remove them all.

<#
.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

This is the simple form of a more generalized script that can accept a parameter of either -All or a list of mapped drive letters to remove. (And yes, unmap isn't an approved verb. But this script started out life many, many years ago as a batch file ("unmapdrives.cmd"), so it's still got the same base name it has always had, because that's what my fingers remember!)

2 Responses to Unmapping Network Drives

  • Edwin says:

    Would you have an explanation for the following?

    When i map a new persistent ps drive i somethimes get errors if i try to remove it although my command syntax is correct

    If i specify the drive letter in capitals all works ( see below )

    new-psdrive -name G -psprovider filesystem -root \\192.168.6.101\c$ -persist

    PS C:\> get-psdrive G

    Name Used (GB) Free (GB) Provider Root CurrentLocation
    —- ——— ——— ——– —- —————
    G 33,20 216,80 FileSystem \\192.168.6.101\c$

    PS C:\> remove-psdrive G

    drive is gone now

    Now if i specify the drive letter in lowercase the map works but when i do a get-psdrive it shows 2 G drives ( one in uppercase and one in lowercase ).

    PS C:\> new-psdrive -name g -psprovider filesystem -root \\192.168.6.101\c$ -persist

    PS C:\> get-psdrive g

    Name Used (GB) Free (GB) Provider Root CurrentLocation
    —- ——— ——— ——– —- —————
    g 33,22 216,78 FileSystem \\192.168.6.101\c$
    G 33,22 216,78 FileSystem G:\

    PS C:\> remove-psdrive g
    remove-psdrive : Cannot find drive. A drive with the name ‘G’ does not exist.
    At line:1 char:1
    + remove-psdrive g
    + ~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (G:String) [Remove-PSDrive], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand

    Is this a bug or can you simply not specify the to be mapped drive letter in lowercase ? Thought about asking someone with a bit more experience in this

    • Charlie Russel says:

      I’m certainly NOT seeing this behaviour in PowerShell v5, so I can’t confirm or deny that it’s a problem in some earlier version. There are a couple of things that can cause errors here, though I don’t believe that uppercase v. lowercase should make any difference at all. However, if you’re logged in AS Administrator, that is a different set of PSDrives than logged in normally. Trying to remove a PSDrive created in a normal window requires that the Remove-PSDrive command be run from a normal window, not an elevated one.

      PowerShell should never be case sensitive unless you explicitly specify case-sensitivity in a cmdlet where that’s appropriate (such as Select-String).

Leave a Reply

Your email address will not be published. Required fields are marked *