How to get mapped drives using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

There are few methods to get the mapped network drive using PowerShell.

  • CMD command method.

You can use the cmd command net use in PowerShell to get the mapped drives.

net use

Output

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.

To get the mapped drives on the remote computer,

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

Or

Invoke-Command -ComputerName RemoteComputer -ScriptBlock{Invoke-Expression -Command "Net use"}
  • PowerShell WMI and CimInstance method.

You can also use the PowerShell WMI method with classname Win32_MappedLogicalDisk to get the mapped network drive on the local computer.

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

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

To get the same on the remote computer.

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

Using the CIM method.

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

On the remote system.

Get-CimInstance -ClassName Win32_MappedLogicalDisk –ComputerName RemoteSystem | Select SystemName, DeviceID, ProviderName
  • Get-PSDrive method.

On the local computer, you can run Get-PSDrive PowerShell cmdlet but it will get all the available drives. To get the network drives we need to filter the output as given below.

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

Output

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

To get mapped drives on the remote computer.

Invoke-Command –ComputerName RemoteComputer -ScriptBlock{Get-PSDrive | where{$_.DisplayRoot -match "\\"}}
raja
Updated on 11-Nov-2020 12:00:32

Advertisements