How do I make PowerShell scripts automatically run as an administrator in Windows 10?

I have two computers running Windows 10 Pro. I have the same PowerShell script on both of them in a .ps1 file.

On the first computer, when I right-click on the .ps1 file and select Run with PowerShell, it runs as an administrator (the PowerShell window says Administrator: PowerShell in the header). But on the second computer, it doesn't run as an administrator (the PowerShell window just says PowerShell in the header). This causes some problems with executing the script because I need to run it as an administrator.

How can I get the second computer to work like the first computer and automatically run the scripts as an administrator when Run with PowerShell is selected?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I am going to go out on a limb and say that on the first machine you are the "administrator", perhaps? When you are a local admin account on a Windows 10 machine, your powershell/cmd prompt are automatically "Administrator". That is the only way I know of for that to be the default behavior.
jrmcanada2Author Commented:
Hi. I'm the administrator on both machines. Neither is on a domain and I'm also the only user on both machines.
Can you tell me if UAC is enabled?
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

jrmcanada2Author Commented:
It's disabled on both machines
Senior IT System EngineerIT ProfessionalCommented:
Try this one:

If (!([bool]([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))) {
	Write-Host "You must be running as an administrator, please restart as administrator"
	Exit 1

Open in new window

jrmcanada2Author Commented:
Sorry but it is still running as non-administrator when I right-click on a .ps1 file and select Run with PowerShell
RyanHelp DeskCommented:
If you right click on the PowerShell icon and select Run as Administrator and then run the script from within PowerShell it should work.  I don't know if there is a way to set it up to run as Adminstrator automatically or not.
Aaron GuilmetteTechnology Solutions ProfessionalCommented:
If the target user isn't an administrator on their machine, they'll have to provide credentials.

You can modify the start properties of a shortcut (or an application) to run as an administrator:
jrmcanada2Author Commented:
The odd thing is that I have a machine that DOES automatically run .ps1's as administrator.

Right now, on the second machine, I have to run PowerShell as administrator and then copy the script in order to run it. This is doable but a little bit of a hassle every time I run it. On the first machine, I just right-click on the script and select Run With PowerShell and it runs as administrator. I'd love to be able to replicate that behaviour on the second machine, but so far I've been unable to.

Aaron, I tried the link above but it didn't change the behaviour. (Also, FWIW, on the first machine - the one that has the behaviour I want - the Run As Administrator is not checked.

My guess is that there's some registry entry that makes this work, but I don't know what it is.
Run regedit and see if this key is the same on both machines:


It should be something like this:

Data: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -p "%1"

Also, see if this key exists:
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Run with PowerShell (Admin)\Command

If so, the "Run with Powershell" command is probably set as follows(see this):

New-PSDrive HKCR Registry HKEY_CLASSES_ROOT | Out-Null
New-Item 'HKCR:\Microsoft.PowerShellScript.1\Shell\Run with PowerShell (Admin)' | Out-Null
New-Item 'HKCR:\Microsoft.PowerShellScript.1\Shell\Run with PowerShell (Admin)\Command' | Out-Null
Set-ItemProperty 'HKCR:\Microsoft.PowerShellScript.1\Shell\Run with PowerShell (Admin)\Command' '(Default)' '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" ""& {Start-Process PowerShell.exe -ArgumentList ''-ExecutionPolicy RemoteSigned -File \"%1\"'' -Verb RunAs}"' | Out-Null

Or, of course, you can copy whatever command is getting it to run as administrator on one machine and copy it to the other registry if the two are different.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jrmcanada2Author Commented:
The first registry entry is identical on both machines. The second one doesn't exist on either machine.

However, the PowerShell commands you referenced have resolved my problem. They have added Run with PowerShell(Admin) as an option in the context menu.

Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.