The shell-friendly way to create shortcut files (*.lnk
) unfortunately does not provide a way to mark a shortcut file as needing to run with elevation (as administrator).
To do so, you have two options:
A quick-and-dirty solution based on raw byte manipulation of a .lnk
file.
- The caveat is that such an approach may break in a future Windows version, should the binary file format of shortcut file changes. See below.
A proper solution via the appropriate COM interfaces, which are not script-friendly, however.
Quick-and-dirty solution, effective as of Windows 10, gratefully adapted from this answer:
# Get the *full path* of the target shortcut file (*.lnk),
# using sample path ~/Desktop/test.lnk here.
$shortcutFile = Convert-Path -LiteralPath (Get-Item ~/Desktop/test.lnk)
# Read the shortcut file's raw byte contents.
$bytes = [System.IO.File]::ReadAllBytes($shortcutFile)
# Set the relevant byte to mark the shortcut as needing to
# run with elevation.
$bytes[21] = 34
# Save the modified byte array back to the original file.
[System.IO.File]::WriteAllBytes($shortCutLocation, $bytes)