Home » Questions » Computers [ Ask a new question ]

how to run a powershell script as administrator

how to run a powershell script as administrator

On my Windows 7 Desktop, I have script.ps1, which needs admin privileges (it starts a service). I want to click on this script and run it with admin privileges.

Asked by: Guest | Views: 270
Total answers/comments: 5
Guest [Entry]

"Here is one way of doing it, with the help of an additional icon on your desktop. I guess you could move the script someone else if you wanted to only have a single icon on your desktop.

Create a shortcut to your Powershell script on your desktop
Right-click the shortcut and click Properties
Click the Shortcut tab
Click Advanced
Select Run as Administrator

You can now run the script elevated by simple double-clicking the new shortcut on your desktop."
Guest [Entry]

"if you are in the same powershell you could do this:

Start-Process powershell -verb runas -ArgumentList ""-file fullpathofthescript"""
Guest [Entry]

"In addition to MDMoore313's answer above:
If we want to execute the commands in the same working directory as we are currently in, we have to add a few things:
#### START ELEVATE TO ADMIN #####
param(
[Parameter(Mandatory=$false)]
[switch]$shouldAssumeToBeElevated,

[Parameter(Mandatory=$false)]
[String]$workingDirOverride
)

# If parameter is not set, we are propably in non-admin execution. We set it to the current working directory so that
# the working directory of the elevated execution of this script is the current working directory
if(-not($PSBoundParameters.ContainsKey('workingDirOverride')))
{
$workingDirOverride = (Get-Location).Path
}

function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

# If we are in a non-admin execution. Execute this script as admin
if ((Test-Admin) -eq $false) {
if ($shouldAssumeToBeElevated) {
Write-Output ""Elevating did not work :(""

} else {
# vvvvv add `-noexit` here for better debugging vvvvv
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -file ""{0}"" -shouldAssumeToBeElevated -workingDirOverride ""{1}""' -f ($myinvocation.MyCommand.Definition, ""$workingDirOverride""))
}
exit
}

Set-Location ""$workingDirOverride""
##### END ELEVATE TO ADMIN #####

# Add actual commands to be executed in elevated mode here:
Write-Output ""I get executed in an admin PowerShell"""
Guest [Entry]

If you want an option to launch a Powershell script as adminstrator, directly from the Explorer context-menu, see section 2 of my answer here: stackoverflow.com/a/57033941/2441655
Guest [Entry]

"Add this to the beginning of the script:

$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if ($testadmin -eq $false) {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file ""{0}"" -elevated' -f ($myinvocation.MyCommand.Definition))
exit $LASTEXITCODE
}"