PowerShell Version 4 New Feature: Requires Statements Now Let Users Require Administrator Access Rights If Needed

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.

ps4-requiresadmin-help

Here’s the message that’s displayed if this option is specified and the person running the script is not an administrator:

ps4-requiresadmin

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:

ps3-requiresadmin

Interested in learning more about PowerShell version 4? See the “What’s New in Windows PowerShell” article on TechNet.

µ

Leave a Reply

%d bloggers like this: