How can you replace multiple UNC paths of mapped drives via registry using PowerShell?

1k Views Asked by At

Our servers team has implemented a DFS, but users across the company still have drives mapped using the server name(s) at various sites. I'd like to push out a PS script that updates a SINGLE registry value (per drive).

My goal is to look through each drive letter key, if the key exists and the remote path starts with the server name then replace it with, the DFS name \\domain.com\SITE\+remainder of the path. This way users keep the same drive letters without having to "remap" their drives

Using Denver office as an example...

$OldServer = "\\denvernas01\"
$NewServer = "\\domain.com\DEN\"

$DriveLetterArray = "A","B","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
    foreach ($DriveLetter in $DriveLetterArray)
        { $Drives = Get-ItemPropertyValue HKCU:\Network\$DriveLetter -Name RemotePath -ErrorAction SilentlyContinue 
          $RemainingPath = $Drives.Replace($OldServer,"")
    foreach ($Drive in $Drives)
        { if ($Drive -like "*$OldServer*")
        { Set-ItemProperty HKCU:\Network\$DriveLetter -Name RemotePath -Value "$NewServer"+"$RemainingPath" }}}

EDIT ^^^This currently works, but only if the server name in RemotePath is all lower case. I.e. the server variables are case sensitive. Any thoughts on how to define the $OldServer & $NewServer variables so it will work with case variations???? e.g. Denvernas01, DENVERNAS01 (or anything inbetween)

I've come across a few threads discussing New-PSDrive, Get-WMIObject, etc, but I'd really like to just replace this one registry value. This would be a good "patch" that would take some stress off of our desktop support team. Trust me - I'll be advocating for GPO to push out common mapped drives once this is all over.

Any feedback is greatly appreciated. Thank you!

1

There are 1 best solutions below

0
aceventura On

If anyone out there is interested, this is what I ended up with, and it worked like a charm. Thought I'd share...

$OldServer = '\\denvernas01'
$NewServer = '\\Domain.com\DEN'

$DriveLetterArray = "A","B","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
foreach ($DriveLetter in $DriveLetterArray){
    $DrivePath = $null; $ConvertedPath = $null
    $DrivePath = Get-ItemPropertyValue -Path "HKCU:\Network\$DriveLetter" -Name "RemotePath" -ErrorAction SilentlyContinue
    if ($DrivePath -eq $null) {continue}
    #Replace Old Drive Path
    if ($DrivePath -like "*${OldServer}*") {
        $ConvertedPath = $DrivePath -ireplace [regex]::Escape("$Oldserver"), $NewServer
        $ConvertedPath
        Set-ItemProperty -Path "HKCU:\Network\$DriveLetter" -Name "RemotePath" -Value "${ConvertedPath}"
    } else { 
    #Write-Host "no match"
    continue
    }
    #Write-Host ""
}
#Remove previous mountpoints 
Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\* | Where-Object Name -Match "##denvernas" | Remove-Item