I have pinned admin cmd and admin pwsh to taskbar, I know how to run .bat files as Administrator, just right-click->"Run as administrator", for .ps1 files, I have modified openwith of .ps1 files so that the default program to open them is PSCore7, but right-clicking on them doesn't show a "Run as administrator" option, in order to run .ps1 as admin, I have to open admin pwsh and cd to $scriptdir (I put all my scripts in one folder) and run them like this:.\script.ps1;

How can I make a script(.bat and .ps1) automatically gain Administrator privileges when running so that I can just double click them and they run as admin automatically?

AND some system folders and registry keys are inaccessible even with admin rights, they are only accessible to TrustedInstaller, I know how to gain access to them, for files and folders, use right-click->Properties->Security->Advanced to open "Advanced Security Settings for *" in explorer to take ownership and change permissions, for registry keys, use right-click->Permissions... in regedit to change permissions, or use icacls and cacls in cmd;

But this method is very risky, by using this method, somethings will stop working unpredictably somehow, for instance, taking ownership of C:\Windows\SystemApps will almost definitely render UWP apps unusable, and it can be hard to return ownership to TrustedInstaller;

I have Google searched for a way to run a script as TrustedInstaller, and found this:https://www.winhelponline.com/blog/run-program-as-trustedinstaller-locked-registry-keys-files/ It says I can use third party softwares like AdvancedRun, PowerRun, NSudo and ProcessHacker to run the console as TrustedInstaller and run the script, this is not what I wanted;

I want to modify the script itself to automatically gain TrustedInstaller privileges when running, without any 3rdP softwares, with as little user interaction as possible, so that I can just double click on them and they automatically give themselves TrustedInstaller privileges, how can I achieve this?


1 Answer 1


To execute as System use PsExec.

The parameter to use is PsExec -s.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.