我想用PowerShell为这个可执行文件创建一个快捷方式:
C:\Program Files (x86)\ColorPix\ColorPix.exe如何做到这一点?
发布于 2012-03-14 20:23:30
我不知道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()发布于 2015-03-12 13:45:11
从PowerShell 5.0开始,New-Item、Remove-Item和Get-ChildItem已得到增强,可以支持创建和管理符号链接。New-Item的ItemType参数接受新值SymbolicLink。现在,您可以通过运行New-Item cmdlet在一行中创建符号链接。
New-Item -ItemType SymbolicLink -Path "C:\temp" -Name "calc.lnk" -Value "c:\windows\system32\calc.exe"要小心,a SymbolicLink不同于快捷方式,快捷方式只是一个文件。它们有一个大小(一个小的,只是引用它们所指向的地方),它们需要一个应用程序支持该文件类型才能使用。符号链接是文件系统级别的,所有内容都将其视为原始文件。应用程序不需要特殊支持即可使用符号链接。
无论如何,如果您想使用Powershell创建一个Run As Administrator快捷方式,您可以使用
$file="c:\temp\calc.lnk"
$bytes = [System.IO.File]::ReadAllBytes($file)
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON (Use –bor to set RunAsAdministrator option and –bxor to unset)
[System.IO.File]::WriteAllBytes($file, $bytes)如果任何人想要更改.LNK文件中的其他内容,可以参考official Microsoft documentation。
https://stackoverflow.com/questions/9701840
复制相似问题