Script to list current user's mapped network drives

42,179

Solution 1

The keys you're looking for are located here:

\\HKCU\Network

Each mapped drive is represented by a Registry key named for the drive letter. The properties of the mapping are contained in values - the ones you are most likely to be interested in are:

RemotePath REG_SZ
UserName   REG_SZ

The keys will only exist for Persistent connections, I don't think you can find transient mappings in the registry.

The biggest problem with this approach is that you have to either connect remotely to the users machine while they are logged in or connect and enumerate the user profiles, map those to SID's and then search in the relevant Key beneath HKEY_USERS to find the relevant copy of the users hive. That's going to be a bit of work and could be quite slow if you intend to do it a lot.

If this is a regular support issue then why not provide them with a link to a batch file that does something like:

Net use > \\someserver\someshare\%username%.drives

Then you just look in the share for the Username.drives file for a precise copy of what you need. Throw that into a logon script and you have a regularly refreshed copy but obviously you don't want to do that if you really don't need the info.

Edited to add If you want to pursue this using some Powershell scripting there's a sample script on this blog post by Hugo Peeters that shows how to connect to a remote registry but you will have to figure out how to map the username to their SID so you can choose the correct key under HKEY_USERS. Each user who has a profile on the target machine has a copy of their HKCU key saved under a key named with their SID, you need to find the right one and then pull the Network key information from under that.

Solution 2

Assuming you had a list of all workstations in C:\Workstations.txt, you could use Powershell to query WMI on each computer and output the current mappings of the logged-on user to a text file named after the computer.

# Load list into variable, which will become an array of strings
$computerlist = Get-Content C:\Workstations.txt

# Prompt for credentials that have rights to access WMI on the computers
$cred = Get-Credential

# Loop through each item in the array (each computer in the list of computers we loaded into the variable)
ForEach ($computer in $computerlist)
{
  # Query WMI on computer using credentials and output to file with width setting so we see everything
  Get-WmiObject Win32_NetworkConnection -computerName $computer -credential $cred | Select LocalName,RemoteName | Out-File C:\WorkstationMappings\$env:username.txt -width 120
}
Share:
42,179
Dmart
Author by

Dmart

Updated on September 17, 2022

Comments

  • Dmart
    Dmart over 1 year

    I have a Windows XP/ Server 2003 environment here users have mapped different network drives themselves using arbitrary drive letters. Some of these users do not know how to tell the true UNC path of these drives, and I would like to be able to run a script or program to query those drives and show me the drive letters and the corresponding UNC paths. I would like to see output like "net use" in that user's context so that I can see what drives THEY have mapped. I would need to do this using my own admin account, which is where the difficulty lies. I understand this information would be stored in the HKCU registry? I would love to be able to do this in Powershell, but a vbscript or even a standalone executable would do. Thanks.

  • Kai
    Kai over 14 years
    If you want to name files after usernames instead of computer, change $computer.txt to: $env:username + ".txt"
  • Kai
    Kai over 14 years
    Made the change in the post to output to username.txt
  • John Gardeniers
    John Gardeniers over 14 years
    Even better, use a filename that includes both the user name and computer name. Someone using multiple machines may well have different mappings on each. I certainly do.
  • Dmart
    Dmart over 14 years
    Looks cool, but unfortunately this class is coming back blank for me (no objects returned) on the systems I tried it on. I know there are users logged on them with mapped network drives though.
  • Helvick
    Helvick over 14 years
    Absolutely - you can mount and interact with remote registries but finding the keys you want will take a little work. I've added a link to a blog post that will get you started doing this with Powershell. Mapping to the correct key under HKEY_USERS is going to be the hardest part but if you can map the username to their SID then you're most of the way there.