powershellcommand-lineshortcutshortcut-file

Create a shortcut (.lnk file) 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?


Solution

  • I don't know any native commandlet in PowerShell, but you can use a COM object instead:

    $WshShell = New-Object -COMObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk")
    $Shortcut.TargetPath = "%SystemDrive%\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 "%SystemDrive%\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:

    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()
    

    It accepts arguments as its second parameter.