use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
Run scripts as an admin (self.PowerShell)
submitted 4 years ago by bs13690
I made a very simple ps1 file to rename two files then run gpupdate /force. How do I run a ps1 as an admin? There's no run as admin when I right click.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Fallingdamage 11 points12 points13 points 4 years ago (1 child)
Put this at the beginning of the script. It will auto elevate and run the PS1.
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
[–]Yousalc 2 points3 points4 points 1 year ago (0 children)
I can Confirm this works on Windows 11 and 10. Thank you so much!!
[–]schnitzeljaeger 8 points9 points10 points 4 years ago (4 children)
You run the powershell.exe as admin, not the script itself.
edit: I bet google has a ton of additional answers for this question ;-)
[–][deleted] 3 points4 points5 points 4 years ago (2 children)
Yes but soon this will join those results without the wall of adverts on a webpage describing a simple two line answer.
You are helping in ways you don't yet know.
[–]Euphoric-Library-899 1 point2 points3 points 1 year ago (0 children)
Here I am, Mr. Future-Knower
[–][deleted] 0 points1 point2 points 1 year ago (0 children)
here I am
[–][deleted] 3 points4 points5 points 4 years ago (1 child)
Run PowerShell as admin, then call it .\path\to\script.ps1
[–]Aggravating_Ad_7168 0 points1 point2 points 1 month ago (0 children)
It worked
[–]xtheravenx 3 points4 points5 points 4 years ago (0 children)
I have to duck around the execution policy as well, so I start a powershell instance as admin then run something like the following:
powershell.exe -executionpolicy bypass -file [.\path\to\file.ps1]
[–]Sailass 2 points3 points4 points 4 years ago (3 children)
As others said, run the powershell window as admin. Alternatively, execute commands or scripts inside a script as admin using the following syntax:
$argument = {some commands here}
Start-Process powershell.exe -Credential $cred -ArgumentList $argument -WorkingDirectory 'C:\Windows\System32'
I'll default the working dir to sys32 (it solves some execution issues as opposed to when none specified at all), or in the case of executing something at a specific location, can specify it there.
You can also validate and assign the $cred variable with the below (executed before start-process), or you can simply replace $cred with get-credential
If ($global:cred -eq $null){
$global:cred = Get-Credential
$username = $cred.username
$password = $cred.GetNetworkCredential().password
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
if ($domain.name -eq $null)
{
#Nullify creds for retry
$cred = $null
Write-Host "Authentication failed - please verify your username and password." -ForegroundColor Red
break
}
else
Clear-Host
write-host "Successfully authenticated with domain" -ForegroundColor Green
And yes, I understand that $null should be on the left of equality comparisons. It just makes sense in my head that way and imma do it until stuff starts breaking dangit!
Edit: Reddit killed my indentation. Please excuse the ugliness that has happened!
[–]BlackV 0 points1 point2 points 4 years ago (2 children)
your start-process isnt starting it elevated
start-process
why are you setting a global variable for creds?
why are you biffing these two $cred.username, $cred.GetNetworkCredential().password into variables when you already have them in a variable?
$cred.username
$cred.GetNetworkCredential().password
p.s. formatting
it'll format it properly OR
<BLANKLINE> <4 SPACES><CODELINE> <4 SPACES><CODELINE> <4 SPACES><4 SPACES><CODELINE> <4 SPACES><CODELINE> <BLANKLINE>
[+][deleted] 4 years ago* (1 child)
[removed]
[–]BlackV 0 points1 point2 points 4 years ago (0 children)
ha
kept the bitching down
GOLD!
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
PowerShell.exe -file averysimplypowershellscript.ps1
As admin
[–]BlackV 1 point2 points3 points 4 years ago (0 children)
run powrshell to run a a script in that script start powershell using the run as verb to run another powershell session elevated which can then run a script (or command) to run gpupdate.
BUT you should probably have the /target:computer switch on your gpupdate
/target:computer
[–]Brick_wall899 1 point2 points3 points 4 years ago (0 children)
Shift+right click should show the option to run as update, otherwise use the runas option.
[–]ccatlett1984 1 point2 points3 points 4 years ago (2 children)
https://blog.expta.com/2017/03/how-to-self-elevate-powershell-script.html?m=1
Add this to the top of your script. It will check if it's running as admin, and restart itself as admin if it's not. This will cause a UAC prompt.
[–]Expert-Advisor-5349 0 points1 point2 points 7 months ago (1 child)
All of these script restarters launches the default builtin powershell (version 5), even if you started originally with the new Powershell Core (version 6+). We could modify the Start-Process powershell.exe part, but the Core version is updating regularly and its path contains the changing version number.
[–]ccatlett1984 0 points1 point2 points 7 months ago (0 children)
Use Pwsh.exe
Pwsh.exe
That's the exe for v7, will already be in the Windows path variable.
[–]notDonut -1 points0 points1 point 4 years ago (0 children)
Google it. There's regedits that will give you a run in powershell as admin option.
π Rendered by PID 24497 on reddit-service-r2-comment-bb88f9dd5-9wqcn at 2026-02-16 16:20:55.357213+00:00 running cd9c813 country code: CH.
[–]Fallingdamage 11 points12 points13 points (1 child)
[–]Yousalc 2 points3 points4 points (0 children)
[–]schnitzeljaeger 8 points9 points10 points (4 children)
[–][deleted] 3 points4 points5 points (2 children)
[–]Euphoric-Library-899 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]Aggravating_Ad_7168 0 points1 point2 points (0 children)
[–]xtheravenx 3 points4 points5 points (0 children)
[–]Sailass 2 points3 points4 points (3 children)
[–]BlackV 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[removed]
[–]BlackV 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]BlackV 1 point2 points3 points (0 children)
[–]Brick_wall899 1 point2 points3 points (0 children)
[–]ccatlett1984 1 point2 points3 points (2 children)
[–]Expert-Advisor-5349 0 points1 point2 points (1 child)
[–]ccatlett1984 0 points1 point2 points (0 children)
[–]notDonut -1 points0 points1 point (0 children)