7

In Windows I can set up a shortcut to run application always as administrator: enter image description here enter image description here

I need to create such shortcut from the command line. How can I do that?

2 Answers 2

8

A StackOverflow answer shows how to accomplish this in Powershell. There's no easy interface to add the Run as administrator flag, so it involves flipping a bit in the .LNK binary file.

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\7-Zip File Manager.lnk")
$Shortcut.TargetPath = "C:\Program Files\7-Zip\7zFM.exe"
$Shortcut.Save()

$bytes = [System.IO.File]::ReadAllBytes("$Home\Desktop\7-Zip File Manager.lnk")
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes("$Home\Desktop\7-Zip File Manager.lnk", $bytes)
2
  • Does this also work for non shortcut/lnk files? (e.g.: EXE's)
    – JuliusPIV
    Jul 29, 2022 at 17:19
  • @JuliusPIV , no, this only works for the LNK binary file. Aug 24, 2022 at 16:12
0

try with shortcutJS.bat - it can create a shortcut with "run as admin" tick from command line :

call shortcutJS.bat -linkfile "some.lnk" -target "%cd%\some.bat" -adminpermissions yes

this also changes 21st byte of the file. More information can be found somewhere in this docs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .