In PowerShell version 4, there’s a new “-RunAsAdministrator” option for the #Requires statement that prevents a script from executing unless PowerShell is running as an administrator.
The information about this new option from the PowerShell version 4 “about_Requires” help topic is shown in the following image. To learn more about the requires statement, run “help about_requires” from within PowerShell.
Here’s the message that’s displayed if this option is specified and the person running the script is not an administrator:
Previously, you had to use the .NET Framework to determine if PowerShell was running as an administrator. Here’s a snippet from my event 6 entry in the 2013 Scripting Games advanced track showing how this was accomplished in previous versions of PowerShell:
Interested in learning more about PowerShell version 4? See the “What’s New in Windows PowerShell” article on TechNet.