如何使用 PowerShell 获取映射的驱动器?

powershellmicrosoft technologiessoftware & coding更新于 2026/2/9 22:52:17

使用 PowerShell 获取映射的网络驱动器有几种方法。

  • CMD 命令方法。

您可以在 PowerShell 中使用 cmd 命令 net use 获取映射的驱动器。

net use

输出

PS C:\WINDOWS\system32> net use
New connections will be remembered.
Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           K:        \localhost\shared folder Microsoft Windows Network
OK           L:        \localhost\Shared        Microsoft Windows Network
The command completed successfully.

要获取远程计算机上的映射驱动器,

Invoke-Command-ComputerName-RemoteComputer-ScriptBlock{Net use}

或者

Invoke-Command-ComputerName-RemoteComputer-ScriptBlock{Invoke-Expression-Command"Net use"}
  • PowerShell WMI 和 CimInstance 方法。

您还可以使用 PowerShell WMI 方法和类名 Win32_MappedLogicalDisk 来获取本地计算机上映射的网络驱动器。

Get-WmiObject -ClassName Win32_MappedLogicalDisk | Select PSComputerName, Name,ProviderName

PSComputerName  Name ProviderName
--------------  ---- ------------
DESKTOP-9435KM9 K:   \localhost\shared folder
DESKTOP-9435KM9 L:   \localhost\Shared

在远程计算机上获取相同信息。

Get-WmiObject -ClassName Win32_MappedLogicalDisk –ComputerName RemoteComputer | Select PSComputerName, Name,ProviderName

使用 CIM 方法。

Get-CimInstance -ClassName Win32_MappedLogicalDisk | Select SystemName, DeviceID, ProviderName

在远程系统上。

Get-CimInstance -ClassName Win32_MappedLogicalDisk –ComputerName RemoteSystem | Select SystemName, DeviceID, ProviderName
  • Get-PSDrive 方法。

在本地计算机上,您可以运行 Get-PSDrive PowerShell cmdlet,但它会获取所有可用的驱动器。要获取网络驱动器,我们需要按如下所示过滤输出。

Get-PSDrive | where{$_.DisplayRoot -match "\"}

输出

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
K                 318.16         47.24 FileSystem    \localhost\shared folder
M                 286.07         79.36 FileSystem    \localhost\Shared Folder

获取远程计算机上的映射驱动器。

Invoke-Command –ComputerName RemoteComputer -ScriptBlock{Get-PSDrive | where{$_.DisplayRoot -match "\"}}

相关文章