=^.^=

Use Windows PowerShell to Make Multiple Shortcuts Always Run their Target as Administrator

karma

To always run a shortcut's target under the Administrator account one will typically:

  • Right-click on the shortcut file in File Explorer
  • Click the Properties item in the context menu
  • Click the Advanced button
  • Check the Run as administrator checkbox

However, what is one to do when presented with a large batch of such files? Group-selecting them will reveal a Properties window of limited options, excluding the ability to mass-apply the Run as administrator option to the group.

[attachment-IzbIbr]
Illustrating how to conventionally set the Run as administrator setting on a single shortcut

We can leverage an advanced shortcut editing JScript like shortcutJS.bat from https://github.com/npocmaka/batch.scripts/blob/master/hybrids/jscript/shortcutJS.bat (Download raw script file) which will effectively hex-edit the shortcut files in-place to set the Run as administrator bit in the file's binary code. For posterity the script's source (as of March 2024) will be provided at the end of this article.

Save shortcutJS.bat somewhere within your PATH so it can be easily re-used; I chose to drop my copy into C:\Windows\System.

Now we can run a short PowerShell scriptlet that loops through the current working directory's contents where file names end in *.lnk:

$files = Get-ChildItem ./ -Include *.lnk; foreach ($f in $files) { $filename = $f.FullName shortcutJS.bat -edit $filename -adminpermissions yes }

The following is an exact reproduction of shortcutJS.bat from npocmaka / batch.scripts GitHub repository as of March 8, 2024 to ensure it can not disappear or become broken for the purposes of this article:
@if (@X)==(@Y) @end /* JScript comment @echo off cscript //E:JScript //nologo "%~f0" "%~nx0" %* exit /b %errorlevel% @if (@X)==(@Y) @end JScript comment */ var args=WScript.Arguments; var scriptName=args.Item(0); //var adminPermissions= false; var edit= false; function printHelp() { WScript.Echo(scriptName + " -linkfile link -target target [-linkarguments linkarguments] "+ " [-description description] [-iconlocation iconlocation] [-hotkey hotkey] "+ " [-windowstyle 1|3|7] [-workingdirectory workingdirectory] [-adminpermissions yes|no]"); WScript.Echo(); WScript.Echo(scriptName + " -edit link [-target target] [-linkarguments linkarguments] "+ " [-description description] [-iconlocation iconlocation] [-hotkey hotkey] "+ " [-windowstyle 1|3|7] [-workingdirectory workingdirectory] [-adminpermissions yes|no]"); WScript.Echo(); WScript.Echo(scriptName + " -examine link"); WScript.Echo(); WScript.Echo(" More info: http://msdn.microsoft.com/en-us/library/xk6kst2k%28v=vs.84%29.aspx "); } // reads the given .lnk file as a char array function getlnkChars(lnkPath) { // :: http://www.dostips.com/forum/viewtopic.php?f=3&t=3855&start=15&p=28898 :: var ado = WScript.CreateObject("ADODB.Stream"); ado.Type = 2; // adTypeText = 2 ado.CharSet = "iso-8859-1"; // code page with minimum adjustments for input ado.Open(); ado.LoadFromFile(lnkPath); var adjustment = "\u20AC\u0081\u201A\u0192\u201E\u2026\u2020\u2021" + "\u02C6\u2030\u0160\u2039\u0152\u008D\u017D\u008F" + "\u0090\u2018\u2019\u201C\u201D\u2022\u2013\u2014" + "\u02DC\u2122\u0161\u203A\u0153\u009D\u017E\u0178" ; var fs = new ActiveXObject("Scripting.FileSystemObject"); var size = (fs.getFile(lnkPath)).size; var lnkBytes = ado.ReadText(size); ado.Close(); var lnkChars=lnkBytes.split(''); for (var indx=0;indx<size;indx++) { if ( lnkChars[indx].charCodeAt(0) > 255 ) { lnkChars[indx] = String.fromCharCode(128 + adjustment.indexOf(lnkChars[indx])); } } return lnkChars; } function hasAdminPermissions(lnkPath) { return (getlnkChars(lnkPath))[21].charCodeAt(0) == 32 ; } function setAdminPermissions(lnkPath , flag) { lnkChars=getlnkChars(lnkPath); var ado = WScript.CreateObject("ADODB.Stream"); ado.Type = 2; // adTypeText = 2 ado.CharSet = "iso-8859-1"; // right code page for output (no adjustments) //ado.Mode=2; ado.Open(); // setting the 22th byte to 32 if (flag) { lnkChars[21]=String.fromCharCode(32); } else { lnkChars[21]=String.fromCharCode(0); } ado.WriteText(lnkChars.join("")); ado.SaveToFile(lnkPath, 2); ado.Close(); } function examine(lnkPath) { var fs = new ActiveXObject("Scripting.FileSystemObject"); if (!fs.FileExists(lnkPath)) { WScript.Echo("File " + lnkPath + " does not exist"); WScript.Quit(2); } var oWS = new ActiveXObject("WScript.Shell"); var oLink = oWS.CreateShortcut(lnkPath); WScript.Echo(""); WScript.Echo(lnkPath + " properties:"); WScript.Echo(""); WScript.Echo("Target:" + oLink.TargetPath); WScript.Echo("Icon Location:" + oLink.IconLocation); WScript.Echo("Description:" + oLink.Description); WScript.Echo("Hotkey:" + oLink.HotKey ); WScript.Echo("Working Directory:" + oLink.WorkingDirectory); WScript.Echo("Window style:" + oLink.WindowStyle); WScript.Echo("Admin Permissions:" + hasAdminPermissions(lnkPath)); WScript.Quit(0); } if (WScript.Arguments.Length==1 || args.Item(1).toLowerCase() == "-help" || args.Item(1).toLowerCase() == "-h" ) { printHelp(); WScript.Quit(0); } if (WScript.Arguments.Length % 2 == 0 ) { WScript.Echo("Illegal arguments "); printHelp(); WScript.Quit(1); } if ( args.Item(1).toLowerCase() == "-examine" ) { var linkfile = args.Item(2); examine(linkfile); } if ( args.Item(1).toLowerCase() == "-edit" ) { var linkfile = args.Item(2); edit=true; } if(!edit) { for (var arg = 1;arg<5;arg=arg+2) { if ( args.Item(arg).toLowerCase() == "-linkfile" ) { var linkfile = args.Item(arg+1); } if (args.Item(arg).toLowerCase() == "-target") { var target = args.Item(arg+1); } } } if (typeof linkfile === 'undefined') { WScript.Echo("Link file not defined"); printHelp(); WScript.Quit(2); } if (typeof target === 'undefined' && !edit) { WScript.Echo("Target not defined"); printHelp(); WScript.Quit(3); } var oWS = new ActiveXObject("WScript.Shell"); var oLink = oWS.CreateShortcut(linkfile); if(typeof target === 'undefined') { var startIndex=3; } else { var startIndex=5; oLink.TargetPath = target; } for (var arg = startIndex ; arg<args.Length;arg=arg+2) { if (args.Item(arg).toLowerCase() == "-linkarguments") { oLink.Arguments = args.Item(arg+1); } if (args.Item(arg).toLowerCase() == "-description") { oLink.Description = args.Item(arg+1); } if (args.Item(arg).toLowerCase() == "-hotkey") { oLink.HotKey = args.Item(arg+1); } if (args.Item(arg).toLowerCase() == "-iconlocation") { oLink.IconLocation = args.Item(arg+1); } if (args.Item(arg).toLowerCase() == "-windowstyle") { oLink.WindowStyle = args.Item(arg+1); } if (args.Item(arg).toLowerCase() == "-workingdirectory" || args.Item(arg).toLowerCase() == "-workdir") { oLink.WorkingDirectory = args.Item(arg+1); } if (args.Item(arg).toLowerCase() == "-adminpermissions") { if(args.Item(arg+1).toLowerCase() == "yes") { var adminPermissions= true; } else if(args.Item(arg+1).toLowerCase() == "no") { var adminPermissions= false; } else { WScript.Echo("Illegal arguments (admin permissions)"); WScript.Quit(55); } } } oLink.Save(); if (!(typeof adminPermissions === 'undefined')) { setAdminPermissions(linkfile ,adminPermissions); }

Comments

There are no comments for this item.