如何使用 PowerShell 创建快捷方式 [英] How to create a shortcut using PowerShell

查看:69
本文介绍了如何使用 PowerShell 创建快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 PowerShell 为这个可执行文件创建一个快捷方式:

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

C:Program Files (x86)ColorPixColorPix.exe

如何做到这一点?

推荐答案

我不知道 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()

这篇关于如何使用 PowerShell 创建快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆