Powershell Equivalent of NET USE /DELETE?

Andy Cheetham 0 Reputation points

I've been searching through the PowerShell documentation to find a way to clean up my client session connections.

I can use Get-SMBConnection to list open connections but there is no corresponding delete/close-Connection

I can use the NET USE /DELETE command interactively - is there a PowerShell equivalent?

A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
1,316 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 41,606 Reputation points

    It's a two-step process in PowerShell.

    1. Remove-SMBMapping
    2. 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:

    Unmaps network drives
    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. 
    Unmaps all currently mapped network drives 
        Author: Charlie Russel
     Copyright: 2015 by Charlie Russel
              : Permission to use is granted but attribution is appreciated
       Initial: 06/27/2015 (cpr)
    # 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"
    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!!!

    0 comments No comments