Powershell Equivalent of NET USE /DELETE?

Andy Cheetham 0 Reputation points
2023-09-07T08:19:01.52+00:00

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?

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

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,551 Reputation points
    2023-09-07T15:10:31.4666667+00:00

    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:

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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.