如何使用PowerShell创建快捷方式

cet*_*int 82 powershell command-line shortcut desktop-shortcut

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

C:\Program Files (x86)\ColorPix\ColorPix.exe
Run Code Online (Sandbox Code Playgroud)

如何才能做到这一点?

CB.*_*CB. 122

我不知道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()
Run Code Online (Sandbox Code Playgroud)

您可以在$ 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()
Run Code Online (Sandbox Code Playgroud)

并称之为这样

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"  
Run Code Online (Sandbox Code Playgroud)

$ 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()
Run Code Online (Sandbox Code Playgroud)

  • 当在 *true* 桌面上创建快捷方式时(而不是假设一个可能正确也可能不正确的硬编码路径,这是我多次观察到的弊端),WScript 对象的 `SpecialFolders` 方法可能会派上用场:`$ WshShell.SpecialFolders("Desktop")` 将为您提供桌面文件夹的真实路径,您可以在随后调用 `CreateShortcut` 时使用该路径。 (5认同)

JPB*_*anc 37

开始的PowerShell 5.0 New-Item,Remove-ItemGet-ChildItem已增强,以支持创建和管理符号链接.该ItemType的进行参数New-Item接受一个新值,SymbolicLink.现在,您可以通过运行New-Item cmdlet在一行中创建符号链接.

New-Item -ItemType SymbolicLink -Path "C:\temp" -Name "calc.lnk" -Value "c:\windows\system32\calc.exe"
Run Code Online (Sandbox Code Playgroud)

要小心,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)
Run Code Online (Sandbox Code Playgroud)

如果有人想要更改.LNK文件中的其他内容,您可以参考Microsoft官方文档.

  • 令人惊讶的是,PowerShell Dev 宁愿让我们编写如上所示的扭曲且难以理解的代码,而不是仅仅将这 3 行代码实现到一个新参数中,如下所示:“New-Item -ItemType SymbolicLink -RunAsAdmin ...”。 (5认同)
  • 但是,符号链接与快捷方式非常不同.`不会在浏览器中显示发送到菜单,例如,并且不允许自定义快捷方式的属性,如图标或:符号链接使用'新Item`在`"\微软\的Windows \的SendTo $ {AppData的ENV}"创建工作目录. (4认同)
  • 是否也可以设置快捷方式的图标? (3认同)
  • @Luke,符号链接没有指定的文件扩展名 - 您可以自由选择任何文件名,带或不带扩展名 - 尽管在创建到 _file_ 的符号链接的情况下,建议(但技术上没有必要)使用与目标文件_相同_扩展名,尽管当目标是_可执行_时,如果您想_模拟_快捷方式文件,_省略_扩展名可能是最佳选择。最好避免选择“.lnk”作为扩展名,因为它会错误地建议快捷方式文件。JPBlanc,请考虑在您的示例中仅使用“-Name calc”代替“-Name calc.lnk”。 (3认同)