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 /delI 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 YDo not include a colon or a backslash; just use the drive letter only.
2Changed my answer based on your comment
6Net use Y: /delete
In PowerShell 5 (Windows 10) and above, you can use:
Remove-SmbMapping -LocalPath "Y:" 1