Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a shortcut using PowerShell

I want to create a shortcut with PowerShell for this executable:

C:\Program Files (x86)\ColorPix\ColorPix.exe 

How can this be done?

like image 255
cethint Avatar asked Mar 14 '12 12:03

cethint


People also ask

How do you create a symbolic link in PowerShell?

Call the New-Item cmdlet to create symbolic links and pass in the item type SymbolicLink . Next, replace the Link argument with the path to the symbolic link we want to make (including the file name and its extension). Finally, replace the Target portion with the path (relative or absolute) that the new link refers to.


1 Answers

I don't know any native cmdlet in powershell but you can use com object instead:

$WshShell = New-Object -comObject WScript.Shell $Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk") $Shortcut.TargetPath = "C:\Program Files (x86)\ColorPix\ColorPix.exe" $Shortcut.Save() 

you can create a powershell script save as set-shortcut.ps1 in your $pwd

param ( [string]$SourceExe, [string]$DestinationPath )  $WshShell = New-Object -comObject WScript.Shell $Shortcut = $WshShell.CreateShortcut($DestinationPath) $Shortcut.TargetPath = $SourceExe $Shortcut.Save() 

and call it like this

Set-ShortCut "C:\Program Files (x86)\ColorPix\ColorPix.exe" "$Home\Desktop\ColorPix.lnk" 

If you want to pass arguments to the target exe, it can be done by:

#Set the additional parameters for the shortcut   $Shortcut.Arguments = "/argument=value"   

before $Shortcut.Save().

For convenience, here is a modified version of set-shortcut.ps1. It accepts arguments as its second parameter.

param ( [string]$SourceExe, [string]$ArgumentsToSourceExe, [string]$DestinationPath ) $WshShell = New-Object -comObject WScript.Shell $Shortcut = $WshShell.CreateShortcut($DestinationPath) $Shortcut.TargetPath = $SourceExe $Shortcut.Arguments = $ArgumentsToSourceExe $Shortcut.Save() 
like image 134
CB. Avatar answered Oct 12 '22 08:10

CB.