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 I do that?

+59
command-line powershell shortcut desktop-shortcut
Mar 14 '12 at 12:19
source share
2 answers

I don't know any native cmdlet in powershell, but you can use the 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 powershell script file save as set-shortcut.ps1 in $ pwd

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

and name it as follows

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

If you want to pass arguments to the target exe, this can be done with:

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

to $ Shortcut.Save ().

For convenience, a modified version of set-shortcut.ps1 is provided here. It takes 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() 
+82
Mar 14 '12 at 12:23
source share
β€” -

Back to top PowerShell 5.0 New-Item , Remove-Item and Get-ChildItem have been expanded to support the creation and management of symbolic links. The ItemType parameter for New-Item takes a new value, SymbolicLink. Now you can create symbolic links on a single line by running the New-Item cmdlet.

 New-Item -ItemType SymbolicLink -Path "C:\temp" -Name "calc.lnk" -Value "c:\windows\system32\calc.exe" 

Be careful . SymbolicLink is different from a shortcut; shortcuts are just a file. They are sized (small, which simply refers to where they point), and their application requires an application to support this type of file. A symbolic link is the level of the file system, and everyone sees it as a source file. The application does not need special support for using a symbolic link.

Anyway, if you want to create the Run as administrator shortcut using Powershell, you can use

 $file="c:\temp\calc.lnk" $bytes = [System.IO.File]::ReadAllBytes($file) $bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON (Use –bor to set RunAsAdministrator option and –bxor to unset) [System.IO.File]::WriteAllBytes($file, $bytes) 

If someone wants to change something else in the .LNK file, you can refer to the official Microsoft documentation.

+18
Mar 12 '15 at 5:45
source share



All Articles