If you need to run a PowerShell script with administrator permissions, you can check directly in your PS code whether the current PowerShell.exe-process has high permissions.
The following PowerShell code can be used to verify that the current script is running under Run as Administrator :
LetterHost Check high privileges…
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] Administrator)) {
Write warning Not enough permissions to execute this script. Open the PowerShell console as administrator and run the script again.
Pause
}
more {
Write-Host Code runs as administrator – Continue running the script… -ForegroundColor green
}
Save the PowerShell code in the file check_process_elevation.ps1 and run it in the console without administrator privileges:
C:PScheck_process_elevation.ps1
As you can see, a message will appear indicating that you do not have administrator privileges, which means that the PowerShell script has been stopped.
Now run the script in an extended PowerShell session. As you can see, the script has detected that this PowerShell session is running as an administrator.
You can also request the raise directly from a PowerShell script. To do this, instead of a line:
Written warning Insufficient rights.
use the following code:
Starting the Powershell Process -ArgumentList $PSCommandPath -Connections
If you run the script without administrator privileges, it will run again in a new PowerShell session with high privileges and you will see a prompt to increase the UAC. If you accept it, your PS1 script will run as an administrator. (The path to the current PowerShell script file is passed using the $PSCommandPath environment variable.)
With PowerShell 4.0 or later it is even easier to check if your script works as an administrator. Use the -RunAsAdministrator Directive for this.
# required -version 4.0
# required -RunAsAdministrator
Write-Host PowerShell runs as administrator -ForegroundColor Green
If the script does not work under the control of an administrator, the following error is displayed:
The script check_process_elevation.ps1 cannot be executed because it contains the #requirements statement to be executed as administrator. The current Windows PowerShell session does not work as an administrator. Start Windows PowerShell with the Run as Administrator option and then try to run the script again.
Online:1 char:1
+ C:PScheck_process_elevation.ps1
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : Allow: (check_process_elevation.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : Procurement Scenario Procurement Scenario Procurement Scenario Procurement Scenario Procurement Scenario Procurement Scenario Procurement Scenario Procurement Scenario Procurement Scenario Procurement Scenario Scenario Procurement Scenario Scenario Procurement Scenario
When you run the script on a PowerShell v2 computer, you will get the following error message:
The #require operator in the second line cannot be processed because it is not in the correct format.
The required text must be in one of the following formats:
#frequencies -shellid
#frequencies -shellid
#frequencies -pssnapin [-version ].
Another task that may be required for Active Directory management is to use the PowerShell script to check if the current user has domain administrator rights. Use the following code:
If(([Security.Principal.WindowsPrincipal]::GetCurrent()).IsInRole(Domain Admins))
{
# the user running the script has domain administrator rights
}
Otherwise
{
# no domain administrator rights
}
Related Tags:
powershell check user privileges,powershell requires -runasadministrator,powershell exit script,powershell if not,get powershell version,test-isadmin,#requires -runasadministrator,powershell check for admin prompt,powershell ensure admin,what is administrator powershell,run powershell script as administrator from cmd,powershell check if user has local admin rights,powershell elevate to admin in script,powershell run as administrator,powershell confirm administrator,powershell check if script is running as system,powershell test admin credentials