[PowerShell] I want to run PowerShell with administrator privileges!A script that automatically elevates administrator privileges just by embedding it at the beginning of the source!

Windows

Hello!This time, I will show you how to create a Powershell script that can be "executed as administrator" by simply embedding the copied code at the beginning of the PowerShell source!

Even if you right-click and select "Run PowerShell" normally, the powershell script (.ps1) will not be executed with administrator privileges.However, by incorporating this method, you will be able to "execute as administrator privileges" regardless of the startup method.

When you want to create a PowerShell program that performs operations that require administrator privileges, or when you want to distribute a ".ps1" file to someone else to execute it, you can "promote PowerShell to administrator privileges". It's a method that can reduce the number of "one-time effort", no, unexpectedly difficult "execute PowerShell as administrator authority" operation for strangers!

Thing you want to do

● Right-click on the PowerShell script file (.ps1 file)When "Run with PowerShell", it is automatically executed as administrator authority.

* Of course, even if you call and execute the .ps1 file from a batch file (.bat format) file, you will have administrator privileges.

● The aboveJust add one line to the beginning of the ".ps1 file" sourceRealize with.

Script example

if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Administrators")) { Start-Process powershell.exe "-File `"$PSCommandPath`"" -Verb RunAs; exit }

Commentary

Easy to use.All you have to do is paste the copied command into the very first line of your PowerShell program!

If you put it in one line, it may look complicated unexpectedly, so let's break it down into meaningful parts and explain it as follows.

① if (! ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity] :: GetCurrent ()). IsInRole ("Administrators")) {
② Start-Process powershell.exe "-File "$PSCommandPath"" -Verb RunAs exit
}

① Check the current authority and branch the process

① if (! ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity] :: GetCurrent ()). IsInRole ("Administrators")) 

The main point here is the acquisition of authority. "[Security.Princial.WindowsPrincipal] [Security.Principal.WindowsIdentity] :: GetCurrent ()"Has permission to run PowerShell.

In addition, () of "if" at the beginning of the sentence indicates a condition. Since it has a "!", It is a negative sentence.
Normally, "if" means "if", but it is a symbol that means the opposite.In other words, "if not".

Following that, ".IsInRole ("Administrators")Is attached to the back.
This is a method that "IsInRole (○○)" determines the group to which the user belongs.This allows you to determine if you are included in the "○○ User Group". (If included, the answer is "TRUE", that is, "YES".)
* "Administrators" is a group of users who have the right holder authority.

In other words, if you read the code of ①, "If the authority to execute PowerShell at that time is not included in the Administrators group (= administrator authority),".

(1) Start yourself (the .psXNUMX file itself) as a separate process with administrator privileges.

② Start-Process powershell.exe "-File "$PSCommandPath"" -Verb RunAs

"Start-Process powershell.exeIs launching PowerShell as a new process.

At that time, special functions are given by the options (-File ~~ and -Verb RunAs) attached to the back.

"-File "$ PSCommandPath"When starting a process, "specifies the file to start to itself (the .ps1 file itself).In other words, "the .ps1 file itself = $ PSCommandPath" is "executed as PowerShell".

"-Verb RunAs"" Means "as an administrator".By attaching this, you can run as an administrator.


To summarize ① and ② above,If your current privileges are not administrator privileges, start yourself (the .ps1 file itself) as a new process with administrator privilegesIt's a process, which means "just run it normally as PowerShell and it will automatically rerun with administrator privileges"!

Afterword

This time, I introduced how to create a powershell script that automatically executes as administrator authority.

To run the PowerShell console with administrator privileges, press "Win key + X key" at the same time and then press "A key" to directly start "PowerShell with administrator privileges" or "PowerShell.exe". There are several types, such as setting "Always run as administrator" in the property itself, but all of them take a little time and effort.

Even if you can do that, you still have to specify the .ps1 file with a command and execute it.
This is unexpectedly troublesome.

As a magic, just add one line to the beginning of the .ps1 file in advance, and it will save you the trouble, so it is highly recommended.

Also, when creating a PowerShell script that is executed regularly, it may be possible to reduce the number of mistakes by including one line of this magic.

I hope it helps you.

Comment

  1. […] […]

Translate »
I copied the title and URL