首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用PowerShell创建快捷方式

如何使用PowerShell创建快捷方式
EN

Stack Overflow用户
提问于 2012-03-14 20:19:17
回答 1查看 195.7K关注 0票数 109

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

代码语言:javascript
复制
C:\Program Files (x86)\ColorPix\ColorPix.exe

如何做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-14 20:23:30

我不知道powershell中有什么本机cmdlet,但你可以使用com对象:

代码语言:javascript
复制
$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

代码语言:javascript
复制
param ( [string]$SourceExe, [string]$DestinationPath )

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($DestinationPath)
$Shortcut.TargetPath = $SourceExe
$Shortcut.Save()

然后这样叫它

代码语言:javascript
复制
Set-ShortCut "C:\Program Files (x86)\ColorPix\ColorPix.exe" "$Home\Desktop\ColorPix.lnk"

如果要将参数传递给目标exe,可以通过以下方法完成:

代码语言:javascript
复制
#Set the additional parameters for the shortcut  
$Shortcut.Arguments = "/argument=value"  

在$Shortcut.Save()之前。

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

代码语言:javascript
复制
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()
票数 163
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9701840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档