# This will find/replace all strings in shortcut paths, # Tried searching recursively under the user's profile, but found it did not get all the way down to the "Recent" folder # There is a 2nd run for "Recent" files, and also the Public Desktop # Only replaces the string you enter, it will leave the rest of the path in tact. # Run it in a CMD/BAT: # powershell -ExecutionPolicy ByPass -File shortcut-fix.ps1 # Or to run hidden: powershell -ExecutionPolicy ByPass -windowstyle hidden -File shortcut-fix.ps1 # -------------------------------------- # Edit these Variables: $FindOld = "\\svr04" $ReplaceNew = "\\svr07" # A couple of different searchpath options below #Public Desktop: $SearchPath3 = [Environment]::GetFolderPath("CommonDesktopDirectory") $SearchPath1 = $ENV:UserProfile $SearchPath2 = [Environment]::GetFolderPath("Recent") $SearchPath3 = [Environment]::GetFolderPath("CommonDesktopDirectory") # End Variables # -------------------------------------- write-host " Searching in: " $SearchPath1 "and" $SearchPath2 write-host " and" $SearchPath3 $shell = new-object -com wscript.shell write-host "Updating shortcut target" -foregroundcolor red -backgroundcolor black # Checking SearchPath1 # -------------------------------------- dir $SearchPath1 -filter *.lnk -recurse | foreach { $lnk = $shell.createShortcut( $_.fullname ) $oldPath= $lnk.targetPath $lnkRegex = "^" + [regex]::escape( $FindOld ) if ( $oldPath -match $lnkRegex ) { $newPath = $oldPath -replace $lnkRegex, $ReplaceNew write-host "Found: " + $_.fullname -foregroundcolor yellow -backgroundcolor black write-host " Replace: " + $oldPath write-host " With: " + $newPath $lnk.targetPath = $newPath $lnk.Save() } } # -------------------------------------- # Checking SearchPath2, for some reason "Recent" files were not checked under the main profile path- # -------------------------------------- dir $SearchPath2 -filter *.lnk -recurse | foreach { $lnk = $shell.createShortcut( $_.fullname ) $oldPath= $lnk.targetPath $lnkRegex = "^" + [regex]::escape( $FindOld ) if ( $oldPath -match $lnkRegex ) { $newPath = $oldPath -replace $lnkRegex, $ReplaceNew write-host "Found: " + $_.fullname -foregroundcolor yellow -backgroundcolor black write-host " Replace: " + $oldPath write-host " With: " + $newPath $lnk.targetPath = $newPath $lnk.Save() } } # -------------------------------------- # Checking SearchPath3, for Public Desktop (Allusers): # -------------------------------------- dir $SearchPath3 -filter *.lnk -recurse | foreach { $lnk = $shell.createShortcut( $_.fullname ) $oldPath= $lnk.targetPath $lnkRegex = "^" + [regex]::escape( $FindOld ) if ( $oldPath -match $lnkRegex ) { $newPath = $oldPath -replace $lnkRegex, $ReplaceNew write-host "Found: " + $_.fullname -foregroundcolor yellow -backgroundcolor black write-host " Replace: " + $oldPath write-host " With: " + $newPath $lnk.targetPath = $newPath $lnk.Save() } } # --------------------------------------