how to start Powershell with administrator mode

how to start Powershell with administrator mode ?

There are multiple ways to start powershell in administrator mode:

1> From GUI – Go to start -> run -> search for “powershell” -> it will show multiple results, then right click on the “Windows Powershell” -> select “run as administrator”.

2> From Schedule task – If you are planning to execute powershell from schedule task then there is option under “general” tab to run the schedule task with “highest privileges”. you can refer “how to create schedule task using powershell” for more detail

3> From Powershell console – You can also use start-process powershell cmdlets to start powershell in administrator mode as below but it will open another powershell console with administrator mode.

if you want to pass you script to new console and keep the execution going on then you can use below code at start of your script.

399-2

Write-Host "Checking for elevation... "
$CurrentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
if (($CurrentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) -eq $false)
{
$ArgumentList = "-noprofile -noexit -file `"{0}`" -Path `"$Path`" -MaxStage $MaxStage"
If ($ValidateOnly) { $ArgumentList = $ArgumentList + " -ValidateOnly" }
If ($SkipValidation) { $ArgumentList = $ArgumentList + " -SkipValidation $SkipValidation" }
If ($Mode) { $ArgumentList = $ArgumentList + " -Mode $Mode" }
Write-Host "elevating"
Start-Process powershell.exe -Verb RunAs -ArgumentList ($ArgumentList -f ($myinvocation.MyCommand.Definition)) -Wait
Exit
}
write-host "in admin mode.."

 

(Visited 913 times, 1 visits today)

Add a Comment

Facebook