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. 

????

4 Answers

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 
1

I would like to suggest using PowerShell's own Remove-PSDrive. For example:

Remove-PSDrive Y 

Do not include a colon or a backslash; just use the drive letter only.

2

Changed my answer based on your comment

Net use Y: /delete

6

In PowerShell 5 (Windows 10) and above, you can use:

Remove-SmbMapping -LocalPath "Y:" 

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy