How to disconnect a network drive using powershell

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

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.


Changed my answer based on your comment

Net use Y: /delete