Windows – Change shortcut targets in bulk

scriptshortcutswindowswindows 8

I have hundreds of shortcuts to websites where the target looks like this:

C:\Users\Herb\AppData\Local\Google\Chrome\Application\chrome.exe www.somesite.com/foo

I just upgraded to Windows 8, and the Chrome executable is now stored in Program Files; so to get these shortcuts to work, I have to change them to this:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" www.somesite.com/foo

Is there any way I can automate this change? I'm a programmer but haven't done much with Windows scripting.

Best Answer

I recently found myself with a similar issue, and decided to script modification of the links as originally requested. Perhaps someone else will find this useful. This is a PowerShell script based on a previously mentioned link, but has some improvements (only triggers on the leading path name, modifies the existing link instead of deleting/creating, has a dry-run mode, etc).

I'm not particularly knowledgeable when it comes to PowerShell, so I welcome any suggestions for improvement:

$oldPrefix = "\\OldServer\Archive\"
$newPrefix = "\\NewServer\Archive\"

$searchPath = "Z:\"

$dryRun = $TRUE

$shell = new-object -com wscript.shell

if ( $dryRun ) {
   write-host "Executing dry run" -foregroundcolor green -backgroundcolor black
} else {
   write-host "Executing real run" -foregroundcolor red -backgroundcolor black
}

dir $searchPath -filter *.lnk -recurse | foreach {
   $lnk = $shell.createShortcut( $_.fullname )
   $oldPath= $lnk.targetPath

   $lnkRegex = "^" + [regex]::escape( $oldPrefix ) 

   if ( $oldPath -match $lnkRegex ) {
      $newPath = $oldPath -replace $lnkRegex, $newPrefix

      write-host "Found: " + $_.fullname -foregroundcolor yellow -backgroundcolor black
      write-host " Replace: " + $oldPath
      write-host " With:    " + $newPath

      if ( !$dryRun ) {
         $lnk.targetPath = $newPath
         $lnk.Save()
      }
   }
}
Related Question