Get Mapped Drives for All Users in PowerShell

PowerShell get mapped drive for all users

Using net use Command

Use the net use command to get mapped drives for all users in PowerShell.

In the above example, we used the net use command to list all the mapped drives for all users on the local machine. Both drive letters (K: and Z:) were mapped to the same user.

Let’s map another drive to a different user and then execute the net use command again to see if the recently mapped drive is also displayed. See the following example.

First, we specified the drive letter (M:) that we wanted to map the network drive to, followed by the UNC path of the resource that we wanted to share. Remember, we can use any letter to map the network drive; it would be successfully done if it was not already used. Don’t forget to enclose the file share within the quotes if it contains spaces, such as \\DESKTOP-NKK10J\"Another Folder".

If you do not want to struggle with what device to map a drive to, you can use the asterisk for a device (here, device means the drive letter).

Using *, we told the Windows to find and map the next available device (drive letter). See the above output; it also informed that the drive Y: was connected to the \\DESKTOP-NKK10JM\Share.

Until now, we used the net use to authenticate the mapped drives as logged-on users. What if we want to authenticate to the shared resource via alternate credentials meaning with a specific username and password? Or what if we’re going to create a device-less connection (No Drive Letter)? Will the net use command be able to retrieve these drives? Let’s find it in the following example.

In the above example, we used the /u parameter to specify the username (Mehvish) followed by the password (12345) for authentication and did not mention the drive letter, but the net use command listed this mapped drive too; hence, the net use can retrieve the mapped drives for all users whether those are device-less or not, or authenticated or not.

We can not only create or view mapped drives for all users but can delete also. Execute the net use /? command to see all the options that can be used with this command.

Is it possible to retrieve certain properties of the mapped drives? Yes, it is; continue with us to explore them below.

Using Get-WmiObject/Get-CimInstace Cmdlets

Use the Get-WmiObject cmdlet to get specific properties for all mapped drives for all users in PowerShell. This solution is preferred if you use any PowerShell version earlier than 3.0.

The Get-WmiObject cmdlet is used to get the instances of WMI (Windows Management Instrumentation) classes or details about the available classes. We used this cmdlet with the Win32_MappedLogicalDisk class to retrieve its instances and access all properties associated with each mapped drive.

These properties include DeviceID, ProviderName, SessionID, FileSystem, Name and Compressed. We specified the Win32_MappedLogicalDis class using the -Class parameter, which we can omit as well, but using this parameter made the code easy to understand.

Then, we piped the output of the Get-WmiObject cmdlet to the Select-Object cmdlet, which is used to select objects or the specified object properties. We used the Select-Object cmdlet to choose each object’s Name and Compressed properties. The Name showed the drive letter while the Compressed displayed Boolean values (True: drive is compressed, False: drive is not compressed).

We can also use the alias of the Get-WmiObject by replacing it with gwmi.

Use the Get-CimInstance cmdlet to get specific properties for all mapped drives for all users in PowerShell. If you are using PowerShell 3.0 or above, you can continue with this solution as an alternative to using the Get-WmiObject cmdlet because the Get-WmiObject has been superseded by the Get-CimInstance cmdlet since PowerShell 3.0.

The Get-CimInstance cmdlet produced the same results as we got in the previous example; this cmdlet retrieves the CIM (Common Information Model) instances of the class from a CIM server. Remember, we can only use the Get-CimInstance cmdlet on Windows platforms.

We got one empty line at the end in the output produced by the Get-WmiObject and Get-CimInstance because these cmdlets did not display any details about the device-less connection that we previously created for the user named Mehvish; see the last example in Use net use Command. These device-less connections are not even visible in File Explorer.

Using Get-SmbMapping Command

Use the Get-SmbMapping cmdlet to get specific properties for all mapped drives for all users in PowerShell.

The Get-SmbMapping cmdlet is used to get the Server Message Block (SMB) client directory mappings made for the server. We used this cmdlet to retrieve the list of SMB mappings and their associated properties for each particular mapped drive.

Then, we piped the output of the Get-SmbMapping cmdlet to the Select-Object cmdlet to select the LocalPath, RemotePath, and Status properties for all the mapped drives, including the device-less connection.

In the above output, the LocalPath showed the local path, which was used to map the remote path on this computer (this computer means where you ran the command). The RemotePath displayed the path accessed from this computer. Finally, the Status property indicates whether it is connected. We would see the OK status if it was connected; otherwise, Disconnected.

Mapped drives are not persistent by default; you have to make them persistent if you want; how? Set the /persistent parameter to yes using the net use command. Setting the /persistent:yes ensures the connection stays around even after the reboot.

Another worth noting point is that Windows remember the persistent settings of the last created connection. If you will set /persistent to yes for one connection, all the subsequent connections will be persistent until you set the /persistent to no as /persistent:no.

Wondering how to make all previously created connections persistent? We can use the net use command as net use /persistent:yes to make all connections persistent in the current session.

Can we know the owner who mapped drives for all users? Can we also get the share names? Yes, see the following example.

The Get-SmbConnection cmdlet established connections from the SMB client to SMB servers. We used this cmdlet to get the owner’s UserName and the ShareName. Remember, the Share was connected twice, with drive letters Z: and Y:, but it would be listed once in the above example. You can find more about this cmdlet here.

Using Get-PSDrive Cmdlet

Use the Get-PSDrive cmdlet to get mapped drives for all users in PowerShell .

Here, we used the Get-PSDrive to get all drives in the current session provided by the FileSystem, which we specified using the -PSProvider parameter. If we would not specify this parameter, then the Get-PSDrive would retrieve the Windows logical drives, session-specified temporary drives, drives exposed by the PowerShell, and persistent mapped network drives.

Learn the difference between temporary and persistent drives ere.

Let’s look at the following example to understand what we said in the last paragraph clearly.

We explored different solutions; you can choose any based on your project needs.

That’s all about how to get mapped drive for all users in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *