Skip to content

Instantly share code, notes, and snippets.

@kaimallea
Created March 22, 2011 15:29
Show Gist options
  • Select an option

  • Save kaimallea/881412 to your computer and use it in GitHub Desktop.

Select an option

Save kaimallea/881412 to your computer and use it in GitHub Desktop.
A PowerShell (v2) script to iterate through a list of computers and identify manual mappings for each user profile. Hopefully, you've followed best practices and won't need to run this in your environment. ;)
<#
Find Manual Drive Mappings
Created Mar. 19 2011 by Kai Mallea ([email protected])
License: http://www.opensource.org/licenses/mit-license.php
#>
# Newline-delimited file containing a list of computers to audit
$computers = Get-Content C:\Computers.txt
# File to output results to
$saveAs = 'D:\ManualMappings.csv'
# Iterate through all computers
foreach ($computer in $computers) {
# Get all sub keys in HKEY_USERS
try {
$HKU = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $computer)
}
# If unable to remotely access registry note in output
catch {
"$computer,N/A,N/A,N/A,$_" | Add-Content $saveAs
continue
}
# Move on to next $computer if no sub keys exist
if ($HKU.SubKeyCount -le 0) { continue }
# Filter keys containing 'Network' sub key
foreach ($key in $HKU.GetSubKeyNames()) {
$keys = $HKU.OpenSubKey($key + '\\Network')
# Move on to the next $key if the 'Network' sub key is empty or non-existent
if ($keys.SubKeyCount -le 0) { continue }
# Iterate through each drive letter (a corresponding key name) and output drive mapping
foreach ($driveletter in $keys.GetSubKeyNames()) {
$values = $keys.OpenSubKey($driveletter)
# Attempt to translate the SID into a friendly username
try {
$sid = New-Object System.Security.Principal.SecurityIdentifier($key)
$user = $sid.Translate([System.Security.Principal.NTAccount])
}
# Fallback to displaying SID
catch {
$user = $key
}
# Output result to file specified in $saveAs
"$computer,$user,$driveletter," + $values.GetValue('RemotePath') | Add-Content $saveAs
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment