Powershell – script to list user’s mapped drive not giving results or error

mappeddrivepowershell

We are in the process of migrating two file servers to a new server. We have mapped drives via user group in group policy. Many users have manually mapped drives and we need to find these mappings. I have created a PowerShell script to run that remotely get the drive mappings. It works on most computers but there are many that are not returning results and I am not getting any error messages. Each workstation on the list creates a text file and the ones that are not returning results have no text in the files. I can ping these machines. If the machine is not turned on, it does come up error message that the RPC server is not available. My domain user account is in a group that is in the local admin account. I have no idea why some are not working.

Here is the script.

# Load list into variable, which will become an array of strings

If( !(Test-Path C:\Scripts)) { New-Item C:\Scripts -ItemType directory }
If( !(Test-Path C:\Scripts\Computers)) { New-Item C:\Scripts\Computers -ItemType directory }
If( !(Test-Path C:\Scripts\Workstations.txt)) { "No Workstations found. Please enter a list of Workstations under Workstation.txt"; Return}
If( !(Test-Path C:\Scripts\KnownMaps.txt)) { "No Mapping to check against. Please enter a list of Known Mappings under KnownMaps.txt"; Return}

$computerlist = Get-Content C:\Scripts\Workstations.txt

# Loop through each item in the array (each computer in the list of computers we loaded into the variable)
ForEach ($computer in $computerlist)
{

   $diskObject =  Get-WmiObject Win32_MappedLogicalDisk -computerName $computer | Select Name,ProviderName | Out-File C:\Tester\Computers\$computer.txt -width 200

}

Select-String -Path C:\Tester\Computers\*.txt -Pattern cmsfiles | Out-File C:\Tester\Drivemaps-all.txt

$strings = Get-Content C:\Tester\KnownMaps.txt

Select-String -Path C:\Tester\Drivemaps-all.txt -Pattern $strings -notmatch -simplematch | Out-File C:\Tester\Drivemaps-nonmatch.txt -Width 200
Select-String -Path C:\Tester\Drivemaps-all.txt -Pattern $strings -simplematch | Out-File C:\Tester\Drivemaps-match.txt -Width 200

Best Answer

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
        }
    }
}