如何使用powershell断开网络驱动器

Fer*_*SBS 15 networking powershell connection

我想用 powershell 断开网络驱动器 (Y:)。该驱动器号被分配/映射到网络位置。有没有一种简单的方法可以做到这一点?

我相信“net use XXX /delete”会做到这一点。

问题是:

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.
Run Code Online (Sandbox Code Playgroud)

为什么当我尝试:

C:\Windows>net use \\192.168.1.108\d  /del
Run Code Online (Sandbox Code Playgroud)

我得到:

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.
Run Code Online (Sandbox Code Playgroud)

???

Zor*_*che 11

在 powershell 中,您可以发出一组这样的命令来断开驱动器的连接,只提供您用于连接的 UNC,并且不知道映射的驱动器号。

棘手的部分是您必须对\字符进行转义才能在 WMI 查询中使用它。

$Drive = Get-WmiObject -Class Win32_mappedLogicalDisk -filter "ProviderName='\\\\192.168.1.108\\d'"
net use $Drive.Name /delete
Run Code Online (Sandbox Code Playgroud)

  • 将 `/y` 添加到 `net use $Drive.Name /delete` 以强制关闭驱动器上所有打开的连接。如果有打开的连接,`net use` 将提示您在无人参与的脚本运行过程中执行您不想要的操作。 (2认同)

Guy*_*mas 6

我想建议使用 PowerShell 自己的 Remove-PSDrive。例如:

Remove-PSDrive Y
Run Code Online (Sandbox Code Playgroud)

不要包含冒号或反斜杠;只使用驱动器号。

  • Remove-psdrive Y 不会完全删除驱动器,并且会留下网络驱动器映射,当您尝试在同一会话中重新创建 ps 驱动器时,该映射将引发错误。您必须使用 net use /delete 才能解决此问题。如果 powershell remove-psdrive 命令实际上删除了驱动器,那就太好了。 (7认同)

Kel*_*ari 5

根据您的评论更改了我的答案

网络使用 Y: /delete