Table of Contents
Using net use
Command
Use the net use
command to get mapped drives for all users in PowerShell.
1 2 3 |
net use |
1 2 3 4 5 6 7 8 9 10 |
New connections will be remembered. Status Local Remote Network ------------------------------------------------------------------------------ OK K: \\DESKTOP-NKK10JM\JavaProject Microsoft Windows Network OK Z: \\DESKTOP-NKK10JM\Share Microsoft Windows Network The command completed successfully. |
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.
1 2 3 4 |
net use M: \\DESKTOP-NKK10JM\AnotherFolder net use |
1 2 3 4 5 6 7 8 9 10 11 |
New connections will be remembered. Status Local Remote Network ------------------------------------------------------------------------------ OK K: \\DESKTOP-NKK10JM\JavaProject Microsoft Windows Network OK M: \\DESKTOP-NKK10JM\AnotherFolder Microsoft Windows Network OK Z: \\DESKTOP-NKK10JM\Share Microsoft Windows Network The command completed successfully. |
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).
1 2 3 4 |
net use * \\DESKTOP-NKK10JM\Share net use |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Drive Y: is now connected to \\DESKTOP-NKK10JM\Share. The command completed successfully. New connections will be remembered. Status Local Remote Network ------------------------------------------------------------------------------ OK K: \\DESKTOP-NKK10JM\JavaProject Microsoft Windows Network OK M: \\DESKTOP-NKK10JM\AnotherFolder Microsoft Windows Network OK Y: \\DESKTOP-NKK10JM\Share Microsoft Windows Network OK Z: \\DESKTOP-NKK10JM\Share Microsoft Windows Network The command completed successfully. |
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.
1 2 3 4 |
net use \\DESKTOP-NKK10JM\"Script Files" /u:Domain\Mehvish 12345 net use |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
New connections will be remembered. Status Local Remote Network ------------------------------------------------------------------------------ OK K: \\DESKTOP-NKK10JM\JavaProject Microsoft Windows Network OK M: \\DESKTOP-NKK10JM\AnotherFolder Microsoft Windows Network OK Y: \\DESKTOP-NKK10JM\Share Microsoft Windows Network OK Z: \\DESKTOP-NKK10JM\Share Microsoft Windows Network OK \\DESKTOP-NKK10JM\Script Files Microsoft Windows Network The command completed successfully. |
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.
1 2 3 4 |
Get-WmiObject -Class Win32_MappedLogicalDisk | Select-Object Name, Compressed |
1 2 3 4 5 6 7 8 |
Name Compressed ---- ---------- K: False M: False Y: False Z: False |
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 withgwmi
.
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.
1 2 3 4 |
Get-CimInstance -Class Win32_MappedLogicalDisk | Select-Object Name, Compressed |
1 2 3 4 5 6 7 8 |
Name Compressed ---- ---------- K: False M: False Y: False Z: False |
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
andGet-CimInstance
because these cmdlets did not display any details about the device-less connection that we previously created for the user namedMehvish
; see the last example in Use net use Command. These device-less connections are not even visible inFile Explorer
.
Using Get-SmbMapping
Command
Use the Get-SmbMapping
cmdlet to get specific properties for all mapped drives for all users in PowerShell.
1 2 3 4 |
Get-SmbMapping | Select-Object LocalPath, RemotePath, Status |
1 2 3 4 5 6 7 8 9 |
LocalPath RemotePath Status --------- ---------- ------ Z: \\DESKTOP-NKK10JM\Share OK K: \\DESKTOP-NKK10JM\JavaProject OK M: \\DESKTOP-NKK10JM\AnotherFolder OK Y: \\DESKTOP-NKK10JM\Share OK \\DESKTOP-NKK10JM\Script Files Disconnected |
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 toyes
using thenet 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
toyes
for one connection, all the subsequent connections will be persistent until you set the/persistent
tono
as/persistent:no
.Wondering how to make all previously created connections persistent? We can use the
net use
command asnet 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.
1 2 3 4 |
Get-SmbConnection | Select-Object UserName, ShareName |
1 2 3 4 5 6 7 |
UserName ShareName -------- --------- DESKTOP-NKK10JM\Mehvish AnotherFolder DESKTOP-NKK10JM\Mehvish JavaProject DESKTOP-NKK10JM\Mehvish Share |
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 .
1 2 3 |
Get-PSDrive -PSProvider FileSystem |
1 2 3 4 5 6 7 8 9 10 11 |
Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- C 35.34 82.59 FileSystem C:\ Users\Mehvish D 19.92 468.36 FileSystem D:\ E 3.82 439.41 FileSystem E:\ K 3.82 439.41 FileSystem \\DESKTOP-NKK10JM\JavaProject M 3.82 439.41 FileSystem \\DESKTOP-NKK10JM\AnotherFolder Y 3.82 439.41 FileSystem \\DESKTOP-NKK10JM\Share Z 3.82 439.41 FileSystem \\DESKTOP-NKK10JM\Share |
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.
1 2 3 |
Get-PSDrive |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- Alias Alias C 35.38 82.56 FileSystem C:\ Users\Mehvish Cert Certificate \ D 19.92 468.36 FileSystem D:\ E 3.82 439.41 FileSystem E:\ Env Environment Function Function HKCU Registry HKEY_CURRENT_USER HKLM Registry HKEY_LOCAL_MACHINE K 3.82 439.41 FileSystem \\DESKTOP-NKK10JM\JavaProject M 3.82 439.41 FileSystem \\DESKTOP-NKK10JM\AnotherFolder Variable Variable WSMan WSMan Y 3.82 439.41 FileSystem \\DESKTOP-NKK10JM\Share Z 3.82 439.41 FileSystem \\DESKTOP-NKK10JM\Share |
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.