0

I have been assigned the job of automating a long list of tasks for developers to load into Intune. One of the tasks is to set the Run As Administrator feature to Visual Studio with a script. I know you can right-click on the exe, go to properties > Advanced and set that setting, but I'm struggling to find a scriptable way to do that. Any suggestions would be appreciated.

Edit: This to update the shortcut to send the command to run as administrator.

Phoenix14830
  • 360
  • 1
  • 8
  • 24
  • Remember the SO rules: [Provide MRE](https://stackoverflow.com/help/minimal-reproducible-example) --- [How to ask](https://stackoverflow.com/help/how-to-ask) --- [Don't ask](https://stackoverflow.com/help/dont-ask) --- [Proper Topic](https://stackoverflow.com/help/on-topic) --- [Why not upload images of code/errors?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) --- [format your post properly](https://stackoverflow.com/help/formatting) --- [Tour](https://stackoverflow.com/tour) – postanote Nov 02 '22 at 00:11
  • What did you search for? What have you tried? What were the results/errors? Show some stuff. **Also of note, this is not the first time this question has been asked.** Several articles exist on the topic. Lastly, it's a choice but, as an operational risk and end-user delivery thing, developers should not be running/developing as admin anyway. They should be doing that as a regular user, and only elevate to admin for dev admin-required tasks. – postanote Nov 02 '22 at 01:01
  • You can just create a reference machine, set whatever you want and export /backup the settings -- shortcuts, and import/install them to a new machine. Just as you would when creating a normal Gold OS reference image. – postanote Nov 02 '22 at 01:02
  • https://stackoverflow.com/questions/28997799/how-to-create-a-run-as-administrator-shortcut-using-powershell and here https://social.technet.microsoft.com/Forums/en-US/d97ae6ae-9d34-4e78-b8ca-c7a7f31ac80c/modify-shortcuts-advanced-properties-with-powershell – postanote Nov 02 '22 at 04:43

1 Answers1

1

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)
mklement0
  • 382,024
  • 64
  • 607
  • 775