且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 PowerShell 创建快捷方式

更新时间:2023-01-31 11:15:25

我不知道 powershell 中有任何本机 cmdlet,但您可以改用 com 对象:

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("$HomeDesktopColorPix.lnk")
$Shortcut.TargetPath = "C:Program Files (x86)ColorPixColorPix.exe"
$Shortcut.Save()

您可以在 $pwd 中创建一个保存为 set-shortcut.ps1 的 powershell 脚本

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

这样称呼它

Set-ShortCut "C:Program Files (x86)ColorPixColorPix.exe" "$HomeDesktopColorPix.lnk"

如果你想向目标exe传递参数,可以通过:

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"  

之前 $Shortcut.Save().

before $Shortcut.Save().

为方便起见,这里是 set-shortcut.ps1 的修改版本.它接受参数作为它的第二个参数.

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