Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

"Sudo !!" invokes previously executed command with administrator privileges in *nix shell. Is there an equivalent in PowerShell?

share|improve this question
up vote 8 down vote accepted

$^ is a variable that expands to the last executed Powershell command.

You can run a command as another user using runas, so the following works:

runas /user:domain\administrator $^

To shorten that up a bit, you can do some magic with aliases. Take a look at this Technet article for more info.

EDIT: One caveat - $^ only executes the first command in a pipeline or multi-command line. If you need to redo an entire command that is peppered with pipes or semicolons, use Invoke-History instead (which defaults to the last full command in its entirety).

share|improve this answer
Actually, $^ is the first token of the previous command. If I dot-source a script with . ./foo.ps1 then $^ is .. This also means that arguments are not contained. Doing anything that exceeds a single token will not work this way. – Joey May 17 '11 at 17:49
ok, does it ask for password after that ? I am trying to avoid embedding plaintext password in my script. – Server System Specialist May 18 '11 at 0:48
Yes, it does. Unless you run the script as an administrator, you'll need to provide credentials to do anything that requires administrator privileges. You may want to look into something like Kixtart to tokenize an embedded password – Hyppy May 18 '11 at 11:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.