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.
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.
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()
}
}
}
}
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
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()
}
}
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
That did it!
Ended up using it this way
Thanks again for all your help!
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"
}
and removed $newPath = $oldPath -replace $lnkRegex, $newPrefixThanks again for all your help!
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.