Skip to content

Instantly share code, notes, and snippets.

@Diagg
Last active February 20, 2025 09:20
Show Gist options
  • Select an option

  • Save Diagg/c3ca51c8a3d9d7665fbaf4252b1346ef to your computer and use it in GitHub Desktop.

Select an option

Save Diagg/c3ca51c8a3d9d7665fbaf4252b1346ef to your computer and use it in GitHub Desktop.
Run task as Trusted installer using Scheduled Task under system account
# Run task as Trusted Installer From system context (Yeah, Intune, SCCM)
# Should also work under Admin context (if not, remove all references to $P).
# Credit due to : https://www.tiraniddo.dev/2019/09/the-art-of-becoming-trustedinstaller.html
$a = New-ScheduledTaskAction -Execute notepad.exe
$P = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrateurs"
#$P = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" #Warning: the admin Group name is localised
Register-ScheduledTask -TaskName 'TestTask' -Action $a -Principal $P
$svc = New-Object -ComObject 'Schedule.Service'
$svc.Connect()
$user = 'NT SERVICE\TrustedInstaller'
$folder = $svc.GetFolder('\')
$task = $folder.GetTask('TestTask')
#Start Task
$task.RunEx($null, 0, 0, $user)
#Kill Task
$task.Stop(0)
#remove task From Task Scheduler
Unregister-ScheduledTask -TaskName 'TestTask' -Confirm:$false
@Diagg
Copy link
Author

Diagg commented Apr 26, 2021

As Notepad is running in a non-interactive session you won't see it on screen, but
you can easily check that Notepad is running in the correct context using task manager:

image

If you need to the same with Powershell Scriptblock under Admin account, check this gist : https://gist.github.com/Diagg/64794cf25be9eeb52809a5b097873676

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment