The challenge
I want t be able to start some application with Admin privileges without the UAC nagg screen prompting. To accomplish this you need to create a task in the Windows Task Scheduler. One of the options is:
to Run with highest privileges. After you have created the task then you create a Shortcut where you start the Task with it’s name and the parameters /run /TN (taskname). Since I don’t like to do this over and over again manually, I have written a function which you can find below. Maybe you can use this as well.
If you found it useful or have something to add please let me know in the comments. Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<# https://learn.microsoft.com/en-us/troubleshoot/windows-client/admin-development/create-desktop-shortcut-with-wsh https://stackoverflow.com/questions/63287152/value-does-not-fall-within-expected-range-when-applying-property-to-windows-10-s #> function Add-CapAdminShortCut { [CmdletBinding()] param ( [Parameter(Mandatory, Position=1)] [string]$AppName, [Parameter(Mandatory,Position=2)] [string]$ExePath ) $ShortcutName = $AppName + ' As Admin' $AppExePath = $ExePath $principalID = $Env:UserDomain + '\' + $Env:USERNAME $RandomPrefix = Get-Random -Minimum 111 -Maximum 999 $TaskName = $AppName + ' - AdminExecTask - ' + $RandomPrefix if (Test-Path $AppExePath){ #exepath exists so continue <# CREATE TASK #> $action = New-ScheduledTaskAction -Execute $AppExePath $principal = New-ScheduledTaskPrincipal -UserID $principalID -RunLevel Highest $settings = New-ScheduledTaskSettingsSet $task = New-ScheduledTask -Action $action -Principal $principal -Settings $settings Register-ScheduledTask $TaskName -InputObject $task <# CREATE SHORTCUT #> $TaskCmd = 'C:\Windows\system32\schtasks.exe' $TaskArgs = ' /run /TN ' + '"' + $TaskName + '"' $WshShell = New-Object -comObject WScript.Shell $Shortcut = $WshShell.CreateShortcut("$Home\Desktop\$ShortcutName.lnk") $Shortcut.TargetPath = $TaskCmd $Shortcut.Arguments = $TaskArgs #windowstyle is not working $Shortcut.WindowStyle = 0 #&&Minimized 0=Maximized 4=Normal $Shortcut.IconLocation = $ExePath $Shortcut.Save() } else { Write-Error "Exe Path is not correct. Please check." return } Write-Host "Task and Shortcut are created succefully." } |