我不知道 powershell 中的任何本机 cmdlet,但您可以改用 com 对象:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk")
$Shortcut.TargetPath = "C:\Program Files (x86)\ColorPix\ColorPix.exe"
$Shortcut.Save()
您可以在 $pwd 中创建一个 powershell 脚本另存为 set-shortcut.ps1
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)\ColorPix\ColorPix.exe" "$Home\Desktop\ColorPix.lnk"
如果你想将参数传递给目标 exe,可以通过以下方式完成:
#Set the additional parameters for the shortcut
$Shortcut.Arguments = "/argument=value"
之前 $Shortcut.Save().
为方便起见,这里是 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()