PowerShell – How to Disconnect a Network Drive

connectionnetworkingpowershell

I would like to disconnect a network drive (Y:) with powershell. That drive letter is assigned / mapped to a network location. Is there a simple way to do that?

I believe "net use XXX /delete" would do that.

The problem is:

C:\Windows>net use
New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           Y:        \\192.168.1.108\d         Microsoft Windows Network
The command completed successfully.

why when I try:

C:\Windows>net use \\192.168.1.108\d  /del

I get:

C:\Windows>net use \\192.168.1.108\d  /del
The network connection could not be found.

More help is available by typing NET HELPMSG 2250.

????

Best Answer

In powershell you could issue a set of commands like this to disconnect a drive, given only the UNC that you used to connect, and not knowing the drive letter that was mapped.

The tricky part is that you have to escape the \ character to use it in the WMI query.

$Drive = Get-WmiObject -Class Win32_mappedLogicalDisk -filter "ProviderName='\\\\192.168.1.108\\d'"
net use $Drive.Name /delete
Related Question