PowerShell is a powerful tool that can be used to manage many tasks. One of the tasks that PowerShell can be used to manage is creating functions. Functions are a way to group related tasks into one place and make them easier to use. One way to create a function is by using the New-Object cmdlet. The New-Object cmdlet allows you to create a new object, which will have the following properties: Name: The name of the function The name of the function Type: The type of function you want to create (module, script, or class) The type of function you want to create (module, script, or class) Parameters: A list of parameters that will be passed into the function when it is created A list of parameters that will be passed into the function when it is created Output: A return value that will be returned by the function when called To create a module, use the New-Module cmdlet. The New-Module cmdlet allows you to create a new module and add its properties and methods. The following example creates a module named MyModule: New-Module -Name MyModule -Type Module


PowerShell can be extremely useful for a lot of everyday tasks as is, but if you need to tweak some functions with a bit of safety in mind, then how do you define a function so that it requires elevation? Today’s SuperUser Q&A post has the answer to a curious reader’s question.

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

The Question

SuperUser reader Vlastimil wants to know how to define a PowerShell function that requires elevation:

How do you define a PowerShell function that requires elevation?

With the following results:

To be completely clear, if I run PowerShell as “user”, then run the aforementioned function system-check, I want the function to elevate in order to be able to execute the command (I want the UAC prompt to appear).

The Answer

SuperUser contributor Ashton has the answer for us:

Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

For example:

To run a specific script from an elevated window:

To run an entire PowerShell session that prompts the 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 [SS64.com]