Windows – Change shortcut arguments via script

applicationpowershellscriptingshortcutwindows

We have a program here that requires every user to have a shortcut on their desktop that points to an ini file. Each file is unique to the user. Recently, we've done a new install of the application on a separate server. I'd like to be able to run a script on the client computers that will look at the current shortcut, alter the server name, then save it to the same location.

I reckon powershell will be necessary for this. Something to the effect of:

$oldargs = # Pull out the args from the current shortcut using voodoo magic
$args = $oldargs -replace "server1", "server2"
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:PUBLIC\Desktop\app.lnk")
$Shortcut.TargetPath = "%ProgramFiles%\appdir\app.exe"
$Shortcut.Arguments = "$args"
$Shortcut.WorkingDirectory = "%ProgramFiles%\appdir"
$Shortcut.IconLocation = "%ProgramFiles%\appdir\pic.ico"
$Shortcut.Save()

Really, I'm just lost on how I pull out the arguments from the current shortcut arguments.

Best Answer

How about:

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:PUBLIC\Desktop\oldapp.lnk")
$oldargs = $Shortcut.Arguments
$Shortcut = $WshShell.CreateShortcut("$env:PUBLIC\Desktop\newapp.lnk")
$Shortcut.TargetPath = "%ProgramFiles%\appdir\app.exe"
$Shortcut.Arguments = $oldargs -replace "server1", "server2"
$Shortcut.WorkingDirectory = "%ProgramFiles%\appdir"
$Shortcut.IconLocation = "%ProgramFiles%\appdir\pic.ico"
$Shortcut.Save()
Related Topic