Create Shortcut With Elevated Rights

Today I needed to create a shortcut for a program in a package. not that big a challenge I thought, as I have created lots of shortcuts during my 16 years deploying Windows and related software. But then the customer said ”oh, can you set the Run as Administrator flag on that?”.

Well, never had to do that before but it turns out that PowerShell once again comes to the rescue!

We can create the shortcut like we normally do, using the WScript.Shell.CreateShortcut method, and the using the System.IO.FileStream to modify the bitstream on the shortcut that controls the elevation prompt.

The following script contains a PowerShell cmdlet that will create shortcuts with or without elevation (Run as Administrator). The last line contains the call to the cmdlet that creates a shortcut for Notepad++ that will have the Run as Administrator flag set.

CreateShortcut -name "Notepad++ Admin" -Target "${env:ProgramFiles(x86)}\Notepad++\notepad++.exe" -OutputDirectory "C:\Users\Public\Desktop" -Elevated True

2014-12-16 14_13_19-2014-12-16 14_13_31-Notepad   Admin Properties2014-12-16 14_13_49-Advanced Properties

The script

 

1 Function CreateShortcut 2 { 3 [CmdletBinding()] 4 param ( 5 [parameter(Mandatory=$true)] 6 [ValidateScript( {[IO.File]::Exists($_)} )] 7 [System.IO.FileInfo] $Target, 8 9 [ValidateScript( {[IO.Directory]::Exists($_)} )] 10 [System.IO.DirectoryInfo] $OutputDirectory, 11 12 [string] $Name, 13 [string] $Description, 14 15 [string] $Arguments, 16 [System.IO.DirectoryInfo] $WorkingDirectory, 17 18 [string] $HotKey, 19 [int] $WindowStyle = 1, 20 [string] $IconLocation, 21 [switch] $Elevated 22 ) 23 24 try { 25 #region Create Shortcut 26 if ($Name) { 27 [System.IO.FileInfo] $LinkFileName = [System.IO.Path]::ChangeExtension($Name, "lnk") 28 } else { 29 [System.IO.FileInfo] $LinkFileName = [System.IO.Path]::ChangeExtension($Target.Name, "lnk") 30 } 31 32 if ($OutputDirectory) { 33 [System.IO.FileInfo] $LinkFile = [IO.Path]::Combine($OutputDirectory, $LinkFileName) 34 } else { 35 [System.IO.FileInfo] $LinkFile = [IO.Path]::Combine($Target.Directory, $LinkFileName) 36 } 37 38 39 $wshshell = New-Object -ComObject WScript.Shell 40 $shortCut = $wshShell.CreateShortCut($LinkFile) 41 $shortCut.TargetPath = $Target 42 $shortCut.WindowStyle = $WindowStyle 43 $shortCut.Description = $Description 44 $shortCut.WorkingDirectory = $WorkingDirectory 45 $shortCut.HotKey = $HotKey 46 $shortCut.Arguments = $Arguments 47 if ($IconLocation) { 48 $shortCut.IconLocation = $IconLocation 49 } 50 $shortCut.Save() 51 #endregion 52 53 #region Elevation Flag 54 if ($Elevated) { 55 $tempFileName = [IO.Path]::GetRandomFileName() 56 $tempFile = [IO.FileInfo][IO.Path]::Combine($LinkFile.Directory, $tempFileName) 57 58 $writer = new-object System.IO.FileStream $tempFile, ([System.IO.FileMode]::Create) 59 $reader = $LinkFile.OpenRead() 60 61 while ($reader.Position -lt $reader.Length) 62 { 63 $byte = $reader.ReadByte() 64 if ($reader.Position -eq 22) { 65 $byte = 34 66 } 67 $writer.WriteByte($byte) 68 } 69 70 $reader.Close() 71 $writer.Close() 72 73 $LinkFile.Delete() 74 75 Rename-Item -Path $tempFile -NewName $LinkFile.Name 76 } 77 #endregion 78 } catch { 79 Write-Error "Failed to create shortcut. The error was '$_'." 80 return $null 81 } 82 return $LinkFile 83 } 84 85 CreateShortcut -name "Notepad++ Admin" -Target "${env:ProgramFiles(x86)}\Notepad++\notepad++.exe" -OutputDirectory "C:\Users\Public\Desktop" -Elevated True

Download the script here: zip CreateElevatedShortcut.zip 1.05 kB
Downloaded 267 times


Comments (%):

  1. Feldu34 says:

    MDT2013-U1
    Hi henrik,
    we have a specificity on the AD domain integration and wonder if it is possible to do so in MTD. Currently MDT integrates the machine on the CD of the site or one that meets at random, out this poses problems at the network level, and we definitely need to specify a DC that is on a different AD Site
    is it possible to specify in MDT him the CD on which all machines must integrate and therefore choose not to leave the MDT DC Site
    thanks advance for his answers

Leave a Reply