Today we continue our series that answers common questions, or questions that have been asked about/of PowerShell. Today we are talking about Run As Administrator. What we intend to provide is one of the many, many ways you can accomplish these same tasks within PowerShell.
All of the solutions are ours and demonstrate the author’s skill and ability level at the time of writing. That is to say, we might not always write the best PowerShell code, however, if you know of a better way we welcome that input.
Let’s start. The question is:
If I am logged in as a regular user can I use PowerShell to launch an application with administrative privileges that way I do not have to log off the user to complete the task?
The Answer:
This is by far the easiest thing you will learn all day. I use this command at least once a day, and was the first command that stuck to memory.
With this command you can run any application under PowerShell as an administrator, that is, as long as you have the authorization from your Systems Admin/IT Security to do so. We always want to do things by the book as it pertains to organizations. The command is:
Start-Process applicationnameorUNCPath -Verb runAs
That is it. With Start-Process
and the argument being an application in System32 only needs the name, whereas an application anywhere else will need to have the UNC path typed out completely. You can add the entire UNC path to the command with quotes encapsulating the path. However, I find it much easier to read, and modify if I put the entire UNC file path in a variable and use the variable in place of the UNC file path as it is holding that data within it. Like so. I like to throw the location into a variable like the one below:
$variable = "C:\Users\Alan\Desktop\DeleteAllFiles.exe" Start-Process $variable -Verb runAs
The -Verb runAs
tells PowerShell to prompt for administrative privileges.