How Do You Define a PowerShell Function That Requires Elevation?

Posted on

PowerShell could be extraordinarily helpful for a lot of on a regular basis duties as is, but when that you must tweak some capabilities with a little bit of security in thoughts, then how do you outline a perform in order that it requires elevation? At present’s SuperUser Q&A put up has the reply to a curious reader’s query.

At present’s Query & Reply session involves us courtesy of SuperUser—a subdivision of Stack Trade, a community-driven grouping of Q&A web pages.

The Query

SuperUser reader Vlastimil desires to know find out how to outline a PowerShell perform that requires elevation:

Since I can not discover any options to Linux’s sudo elevation command, I’ve the next query. How do I outline a PowerShell perform that requires elevation, as in activating a UAC immediate on my Home windows eight.1 Professional, 64-bit system? For instance, say I run the next perform:

With the next outcomes:

To be utterly clear, if I run PowerShell as “consumer”, then run the aforementioned perform system-check, I need the perform to raise so as to have the ability to execute the command (I need the UAC immediate to look).

How do you outline a PowerShell perform that requires elevation?

The Reply

SuperUser contributor Ashton has the reply for us:

To run a particular command from an elevated window:

For instance:

To run a particular script from an elevated window:

To run a complete PowerShell session that prompts the UAC:

A perform to return $True or $False if the present window is operating with elevated permissions:

To make sure a script is just run As Admin, add this to the start:

In PowerShell, the above could be simplified through the use of a #Requires assertion:

Supply: Run with Elevated Permissions []

Have one thing so as to add to the reason? Pontificate within the feedback. Need to learn extra solutions from different tech-savvy Stack Trade customers? Try the total dialogue thread right here.

قالب وردپرس

Leave a Reply