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.
Powershell – Script to list current user’s mapped network drives
powershellscriptingvbscriptwindows-xp
Related Solutions
Or if you prefer PowerShell, one of my techs wrote this which has worked well for us. I'm sure it can cleaned up some but I left in some testing lines to make it easier to play with and customize. This uses Quest tools which you no longer need, especially if you're on PowerShell v2 and SubInACL:
cls
#Add-PSSnapin quest*
#$dirlist = gci -name c:\test -Exclude *.* | sort #my original
$dirlist = gci \\servername\sharename -Exclude *.* | ? { $_.PSIsContainer }
$subinacl = "C:\utils\subinacl.exe"
foreach ($userdir in $dirlist)
{
$username = $userdir.name
$adaccount = Get-QADUser $username
#Verifies user is an active employee, renamed folder to be deleted if not
If (($adaccount.AccountIsDisabled -eq $TRUE) -or (!$adaccount))
{
write-host "$username is not a current employee"
#takeownership
#takeown /f $userdir /R /D Y /A
#rename folder to _DEL_originalname
$newname = "_DEL_$username"
rename-item -path $userdir -newname $newname
}
Else
{
#get full path
Write-Host $userdir.name
#$currentDir = "c:\test\$userdir" #my original
$currentDir = $userdir.FullName # this way you don't dupe the start folder
#takeown /f $userdir /R /D Y /A
#get ACL of folder
$acl = Get-Acl $currentDir
#variable to set new permissions for username of folder
#$permission = "domainname\$userdir",”FullControl”,”ContainerInherit,ObjectInherit”,”None”,”Allow” #original
$permission = "$username@domainname.com",”FullControl”,”ContainerInherit,ObjectInherit”,”None”,”Allow”
$accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $permission
#actually set the permissions
$acl.SetAccessRule($accessRule)
#$acl | Set-Acl $currentDir #my original
Set-Acl $currentDir $acl
#use subinacl to set owner at parent level and below
$params1 = "/file $currentDir /setowner=domainname\$username"
$params2 = "/subdirectories $currentDir\*.* /setowner=domainname\$username"
$params3 = "/subdirectories $currentDir\* /grant=domainname\$username"
$params4 = "/subdirectories $currentDir\* /grant=domainname\administrators=F"
Invoke-Expression "$subinacl $params1" | out-null
Invoke-Expression "$subinacl $params2" | out-null
Invoke-Expression "$subinacl $params3" | out-null
# Invoke-Expression "$subinacl $params4" | out-null
}
}
It appears from some quick testing that Get-WmiObject Win32_MappedLogicalDisk -computerName $computer
has some limitations. From what I'm reading, whether the current user has Administrative privileges to their computer can determine whether that returns anything or not.
Another option may be to go through the registry. This command lists the mapped drives for the current user, but you'd have to use Invoke-Command
in order to execute it remotely.
Invoke-Command -ComputerName $computer -ScriptBlock {Get-ChildItem HKCU:Network}
If you need to get the drive mappings used by all users this appears to work in my testing:
# Connect to registry on remote computer and pull a list of user keys
$computer="remotecomputer"
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $computer)
$UserKeys = $Reg.GetSubKeyNames()
# Loop through the user keys and access the Network subkey, which contains the user's drive mappings
for ($i=0; $i -lt $Reg.SubKeyCount; $i++) {
$NetKey = $Reg.OpenSubKey("$($UserKeys[$i])\\Network")
# If the user has any mapped drives, get the subkeys containing those drives
if ($NetKey.SubKeyCount -gt 0) {
$DriveKeys = $NetKey.GetSubKeyNames()
for ($n=0; $n -lt $DriveKeys.Length; $n++) {
$DriveKey = $Reg.OpenSubKey("$($UserKeys[$i])\\Network\\$($DriveKeys[$n])")
# Output the drive letter and the network path
"Drive Letter: " + $DriveKeys[$n]
"Network Path: " + $DriveKey.GetValue("RemotePath")
# Close each of the loops and conditions
}
}
}
Best Answer
The keys you're looking for are located here:
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:
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:
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.