且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Powershell创建运行方式管理员快捷方式

更新时间:2023-02-03 20:40:15

这个答案是PowerShell翻译的一个很好的答案这个问题
如何使用JScript创建一个使用 ;以管理员身份运行

This answer is a PowerShell translation of an excellent answer to this question How can I use JScript to create a shortcut that uses "Run as Administrator".

简而言之,您需要以字节数组的形式读取.lnk文件。找到字节21(0x15)并将位6(0x20)更改为1.这是RunAsAdministrator标志。然后你把你的字节数组写回到.lnk文件。

In short, you need to read the .lnk file in as an array of bytes. Locate byte 21 (0x15) and change bit 6 (0x20) to 1. This is the RunAsAdministrator flag. Then you write you byte array back into the .lnk file.

在你的代码中,这将是这样:

In your code this would look like this:

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk")
$Shortcut.TargetPath = "C:\Program Files (x86)\ColorPix\ColorPix.exe"
$Shortcut.Save()

$bytes = [System.IO.File]::ReadAllBytes("$Home\Desktop\ColorPix.lnk")
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes("$Home\Desktop\ColorPix.lnk", $bytes)

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

If anybody want to change something else in a .LNK file you can refer to official Microsoft documentation.