Since I can't find any alternative to Linux' sudo
elevation command, I have the following question:
How to define a PowerShell function which requires elevation? I mean UAC prompt.
Say, such function follows:
function system-check {
SFC /ScanNow
}
System:
Windows 8.1 Pro 64-bit
PowerShell:
Major Minor Build Revision ----- ----- ----- -------- 5 0 10586 117
EDIT1:
To be 100% understandable let me rephrase:
- I run PowerShell as user
- I run the aforementioned function
system-check
- I want the function to elevate in order to be able to execute the command; note, that I want the UAC prompt to appear
Best Answer
To run a specific command from an elevated window:
For example:
To run a specific script from an elevated window:
To run an entire PowerShell session prompting for UAC:
A function to return $True or $False if the current window is running with elevated permissions:
To ensure a script is only run As Admin, add this to the beginning:
In PowerShell v4.0 the above can be simplified by using a #Requires statement:
Source: Run with elevated permissions