1

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.

3 Answers 3

4

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.

2
  • This is interesting info, but I am looking for something I can run remotely. Is there any way to get at the HKCU hive remotely?
    – Dmart
    Nov 5, 2009 at 22:31
  • 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.
    – Helvick
    Nov 5, 2009 at 22:41
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
}
4
  • If you want to name files after usernames instead of computer, change $computer.txt to: $env:username + ".txt"
    – Kai
    Nov 5, 2009 at 21:29
  • Made the change in the post to output to username.txt
    – Kai
    Nov 5, 2009 at 21:32
  • 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. Nov 5, 2009 at 22:23
  • 1
    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.
    – Dmart
    Nov 5, 2009 at 22:30
0

In WMI/VBScript the bit you need to list out the current user's mapped drives is:

Set objNetwork = WScript.CreateObject("WScript.Network")
Set colDrives = objNetwork.EnumNetworkDrives
For i = 0 to colDrives.Count-1 Step 2
 Wscript.Echo colDrives.Item(i) & vbTab & colDrives.Item (i + 1)
Next

That obviously only lists them for the current logged on user on the machine that it's run on.

We're running an exercise to discover all user's mapped drives at the moment, and are running a script like this in each user's context to get that info and dump down to a network share where we can grab the files and parse them with another script later:

scriptVersion = "1.0"

Set objShell = CreateObject("WScript.shell")
Set objEnv = objShell.Environment("Process")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")

strUserName = objEnv("USERNAME")
strComputerName = lcase(objEnv("COMPUTERNAME"))
bPath = "\\servername\sharename"    ' Server and path to save files
bFile =  bPath & "\" & strComputerName &"-" & strUserName & "-NetDrives.txt"

'Setup output file
If objFSO.FileExists(bFile) then
    Set objTextFile = objFSO.OpenTextFile(bFile, 8, True)   ' 8 = appending
Else
    Set objTextFile = objFSO.OpenTextFile(bFile, 2, True)   ' 2 = writing
End if

objTextFile.WriteLine Now & " ::: Drive mappings listing v" & scriptVersion

'Loop through current network drives
Set colDrives = objNetwork.EnumNetworkDrives
For i = 0 to colDrives.Count-1 Step 2
 objTextFile.WriteLine colDrives.Item(i) & vbTab & colDrives.Item (i + 1)
Next


Set objShell = Nothing
Set objFSO = Nothing
Set objNetwork = Nothing

Wscript.Quit(ExitErrorLevel)

This can then be put in a login script or run with a software distribution tool, like Microsoft SMS/SCCM.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .