Link to home
Start Free TrialLog in
Avatar of GGHC
GGHC

asked on

How to change a Windows shortcut's "Target" and "Start in" via script from C:\Program Files (x86)\ to Program Files\

We have user that have roaming shortcuts both Chrome and Office apps. They toggle between systems that have apps installed as (x86) or native 64-bit.  This causes issue with the shortcuts.
Many are Chrome apps (using --app=https://.. in the Target)

-How do I programmatically change the "Target" and "Start in" to just replace the ..(x86)\ or add it back without affecting the rest of the path.

- Is it possible that a script can get an inventory of the one that are ..(x86) and the one that aren't from a given path?
   -Possible to do it by using if Exist statements( for example if exist "C:\Program Files\Google\Chrome\   Replace shortcut with

Open to other suggestions. 

Avatar of Adam Leinss
Adam Leinss
Flag of United States of America image

Take a look at my blog post: https://leinss.com/blog/?p=2471

I ran into a similar issue of shortcuts pointing to an old file server and a script to fix them to point them to the new server.
Avatar of GGHC
GGHC

ASKER

Amazing! Exactly what I was hoping to get!
I added the 'Start In' ($lnk.WorkingDirectory ) portion to it.

Trying to simplify the switch between x86 and x64 and vise versa. Below is an example if it detects the x64 "C:\Program Files\Google\Chrome\Application\chrome.exe" the path are updated

Question: What modification can I do so it can change path to x86 if x86 Chrome is detected?


------------------------------------------------

$x86Prefix = "C:\Program Files (x86)\Google\Chrome\"
$x64Prefix = "C:\Program Files\Google\Chrome\"

# Script converts Chrome Path from x86 to x64 if x64 Chrome is detected.
if (Test-Path -Path "C:\Program Files\Google\Chrome\Application\" )
{
$searchPath = "C:\TEMP\Shortcuts\"

$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
   $oldPathStartin= $lnk.WorkingDirectory

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

   if ( $oldPath -match $lnkRegex ) {
      $newPath = $oldPath -replace $lnkRegex, $x64Prefix
     $newPathStartin = $oldPathStartin -replace $lnkRegex, $x64Prefix

      write-host "Found: " + $_.fullname -foregroundcolor yellow -backgroundcolor black
      write-host " ReplacePath: " + $oldPath
      write-host " With:    " + $newPath
     write-host " ReplaceStartin: " + $oldPathStartin
      write-host " With:    " + $newPathStartin

      if ( !$dryRun ) {
         $lnk.targetPath = $newPath
       $lnk.WorkingDirectory = $newPathStartin
         $lnk.Save()
      }
   }
}
}  
SOLUTION
Avatar of Adam Leinss
Adam Leinss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GGHC

ASKER

Thanks Adam!
Like your blog site. Good topics- https://leinss.com/blog/

But Have one more head scratcher it seams Google at some point created "chrome_proxy.exe" as found in some shortcuts. Below is the output of a dry run. 

Found:  + C:\temp\shortcuts\Scratch Notes.lnk
ReplacePath:  + C:\Program Files (x86)\Google\Chrome\Application\chrome_proxy.exe
 With:     + C:\Program Files\Google\Chrome\Application\chrome_proxy.exe

What is a slick way to always make it "..Application\chrome.exe" if it's a x64 Chrome install??


Here is the current working copy
$x64Chrome = "FALSE"
$x86Chrome = "FALSE"

if (Test-Path -Path "C:\Program Files\Google\Chrome\Application\" ) {
$x64Chrome = "TRUE"
$Prefix = "C:\Program Files\Google\Chrome\"
$oldPrefix = "C:\Program Files (x86)\Google\Chrome\"
}

if ($x64Chrome -eq "False") {
$x86Chrome = "TRUE"
$Prefix = "C:\Program Files (x86)\Google\Chrome\"
$oldPrefix = "C:\Program Files\Google\Chrome\"
} 

#$searchPath = "$env:APPDATA\ClassicShell\Pinned\"
$searchPath = "c:\temp\shortcuts\"

$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
   $oldPathStartin= $lnk.WorkingDirectory

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

   if ( $oldPath -match $lnkRegex ) {
      $newPath = $oldPath -replace $lnkRegex, $Prefix
     $newPathStartin = $oldPathStartin -replace $lnkRegex, $Prefix

      write-host "Found: " + $_.fullname -foregroundcolor yellow -backgroundcolor black
      write-host " ReplacePath: " + $oldPath
      write-host " With:    " + $newPath
     write-host " ReplaceStartin: " + $oldPathStartin
      write-host " With:    " + $newPathStartin

      if ( !$dryRun ) {
         $lnk.targetPath = $newPath
       $lnk.WorkingDirectory = $newPathStartin
         $lnk.Save()
      }
   }
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GGHC

ASKER

That did it!
Ended up using it this way
$x64Chrome = "FALSE"
$x86Chrome = "FALSE"


if (Test-Path -Path "C:\Program Files\Google\Chrome\Application\" ) {
$x64Chrome = "TRUE"
$newPrefix = "C:\Program Files\Google\Chrome\"
$oldPrefix = "C:\Program Files (x86)\Google\Chrome\"
}

if ($x64Chrome -eq "False") {
$x86Chrome = "TRUE"
$newPrefix = "C:\Program Files (x86)\Google\Chrome\"
$oldPrefix = "C:\Program Files\Google\Chrome\"
} 

if ($x64Chrome -eq "True") {
   $newPath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
   }else {
   $newPath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
   }


Open in new window

and  removed     $newPath = $oldPath -replace $lnkRegex, $newPrefix

Thanks again for all your help!