How to List User’s Mapped Drives


Sometimes you need to know what drives a user has mapped. The solution is very simple – the list is located in this registry key:

HKEY_USERS\<user SID>\Network

Each subkey in this key represents the mapped drive and the RemotePath value contains the path to the folder.

To make my life a bit easier I have written a script, which lists the mapped drives for all the users logged onto the server. Especially it is useful when runs against the Terminal Services server or against the pool of such servers.

So, here the script is:

#####################################################################
####  LIST-MAPPEDDRIVES
#####################################################################
####  This script lists mapped drives for all users on the
####   particular computer. Very useful for TS servers
####
#####################################################################
####  Author: Andrew Karmadanov. Jun 23, 2010
#####################################################################
#####################################################################

#####################################################################
####   INITIALIZING THE VARIABLES
#####################################################################
$InfoMessage = $Host.PrivateData.WarningForeGroundColor
$ErrorMessage = $Host.PrivateData.ErrorForeGroundColor
$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
############################################################
Write-Host “Enter the name of the file with the computer list.”
$ComputerFileName = Read-Host ”   Hit <Enter> if you want to enter the computer name(s)”
if ($ComputerFileName -eq “”)
{
$ComputerList = Read-Host “Enter the list of computers using comma (,) as a separator”
if ($ComputerList -eq “”)
{return}
$Computers = $ComputerList.Split(“,”)
}
else
{
if ((Test-Path $ComputerFileName) -eq $False)
{
$ComputerFileName = Join-Path $ScriptPath $ComputerFileName
if ((Test-Path $ComputerFileName) -eq $False)
{
Write-Host ($ComputerFileName + ” doesn’t exist”) -foregroundColor $ErrorMessage
return
}
}
$Computers = Get-Content $ComputerFileName
}
############################################################
$OutputFileName = (Read-Host “Enter the result file name”).Trim()
if ($OutputFileName -ne “”)
{
if ($OutputFileName.IndexOf(“:”) -eq -1)
{$OutputFileName = Join-Path $ScriptPath $OutputFileName}
if ((Test-Path $OutputFileName) -eq $True)
{
Write-Host ($OutputFileName + ” already exists”) -foregroundColor $ErrorMessage
return
}
Set-Content $OutputFileName -value (“Computer;User Name;Drive Letter;Drive Path”)
}

#####################################################################
####   MAIN LOOP
#####################################################################
ForEach ($Computer in $Computers)
{
$Filter = “Address=” + [char]34 + $Computer + [char]34
$PingStatus = Get-WMIObject Win32_PingStatus -Filter $Filter

Write-Host
Write-Host ($Computer.ToUpper()) -foregroundColor $InfoMessage
if (($PingStatus -eq $Null) -or ($PingStatus.StatusCode -ne 0))
{
Write-Host (”   ” + $Computer + ” is not pingable”) -foregroundColor $ErrorMessage
Continue
}

#User List
$RootKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(“USERS”,$Computer)
$SubKeyNames = $RootKey.GetSubKeyNames()
ForEach ($SubKeyName in $SubKeyNames)
{
if (($SubKeyName.Length -gt 12) -and ($SubKeyName.Contains(“_Classes”) -ne $True))
{
Write-Host
$Key = $RootKey.OpenSubKey($SubKeyName + [char]92 + “Software\Microsoft\Windows\CurrentVersion\Explorer”)
$UserName = $Key.GetValue(“Logon User Name”)
Write-Host (”   User Name: ” + $UserName)

#Drive List
$NetworkKey = $RootKey.OpenSubKey($SubKeyName + [char]92 + “Network”)
if ($NetworkKey -ne $Null)
{
$MappedDrives = $NetworkKey.GetSubKeyNames()
if ($MappedDrives -ne $Null)
{
ForEach ($MappedDrive in $MappedDrives)
{
$DriveKey = $NetworkKey.OpenSubKey($MappedDrive)
$DrivePath = $DriveKey.GetValue(“RemotePath”)
Write-Host (”      ” + $MappedDrive.ToUpper() + “: = ” + $DrivePath)

if ($OutputFileName -ne “”)
{
$Output = $Computer + “;” + $UserName + “;” + $MappedDrive.ToUpper() + “;” + $DrivePath
Add-Content $OutputFileName -value $Output
}
}
}
else
{Write-Host ”   There is no any mapped drive”}
}
else
{Write-Host ”   Profile error: There is no NETWORK key in the user profile” -foregroundColor $ErrorMessage}
}
}
}

return

Advertisements

One Response to How to List User’s Mapped Drives

  1. Sunny says:

    Great Script, just what I was looking for.. however, the output of user name is showing blank… I’m running this script under Terminal Server (RDS 2008 R2).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: