Using a PowerShell script to run as a different user & elevate the process.


There are times when I want to elevate a PowerShell process on one of our lab Windows 7 clients & run a script as a user account with administrator privileges.

Usually I’m working on a lab machine running as a non-administrative test user. Since you cannot easily elevate and run as a different user from the right-click context menu, I wrote a simple script that I thought I’d share.

Note: One thing to call out is that our lab machines have their Execution Policy set for Unrestricted.

The script:

Start-Process powershell.exe -Credential "TestDomain\Me" -NoNewWindow -ArgumentList "Start-Process powershell.exe -Verb runAs"

The following section starts the PowerShell command-line process with Start-Process prompting for user credentials. You may not need this dependent on UAC settings, as you might already get an over-the-shoulder prompt for creds during elevation.  

Start-Process powershell.exe -Credential "TestDomain\Me"

The -NoNewWindow parameter re-uses the same PowerShell command window.

Here I’m using -ArgumentList parameter to pass in the second Start-Process leveraging –Verb runAs to force the elevation prompt.

I just saved this PowerShell script to a scratch share on the lab machines, and when I need to elevate and run PowerShell as a different user. I’d just double click on the script. It’s not the most elegant code, but it gets the job done and ideally shows some fairly cool optional parameters of Start-Process.  

Comments (14)
  1. Anonymous says:

    @ Brian Ehlert
    For me your suggestion does not work.
    Do you have a working example?
    Thanks

  2. Ayan Mullick says:

    How do I run the commands without the "-Credential" option with admin privileges.

    Such as get-eventlog or get-childitem

  3. Michael Zheng says:

    Hi B_Shy,

    I tried to run another powershell with different credential "mydomainusername". A new window always pop up. Could you help?

    Start-Process powershell -Credential "resourcepro0mzheng" -NoNewWindow

  4. vince tuan says:

    By removing -Credential, Start-Process will not pop a new window.

  5. Chris says:

    I have the same issue as Michael, a new windows pops up.  Removing -Credential, defeats the purpose of this post.

    $creds = Get-Credential

    start-process powershell.exe -Credential $creds -NoNewWindow -ArgumentList "Start-Process powershell.exe -verb runas"

  6. Brian Ehlert says:

    To prevent the popup you need to build a credential object and pass it in when you launch the process:

    $credential = New-Object System.Management.Automation.PsCredential(".administrator", (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force))

  7. SSC says:

    how would I script it if I wanted to run it as an admin (whether it be a local or a domain) without any prompt or window from showing? I have a silent uninstall & installs script I need to push out and I need it to run as admin, thank you.

  8. Chen V says:

    I got this error

    Start-Process : This command cannot be run due to the error: Only part of a ReadProcessMemory or WriteProcessMemory request was completed.

  9. Darren says:

    What if the parameter you are trying to pass through with the -ArgumentList has spaces in it… For example, the .EXE I am running references an external .INI file stored on a network share which needs to be included when run, however that parameter does not get passed through correctly and EXE fails to open.

    I have tried single quotes, I have tried double quotes; I have tried putting the parameter into a variable $Param and using this in the script line, but this also fails with message “command-line parameter is invalid” as soon as first space is reached. Any ideas gents? Thanks

    Script I am using = Start-Process -FilePath $RunPath -ArgumentList $Param

  10. jim says:

    Played with this a bit to get it to do exactly what I want and came up with the following:

    Add-Type -AssemblyName System.Windows.Forms

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.Title = “Run Powershell Script”
    $OpenFileDialog.InitialDirectory = $([Environment]::GetFolderPath(“Desktop”))
    $OpenFileDialog.Filter = “Windows PowerShell Scripts (*.ps1)| *.ps1”
    $OpenFileDialog.ShowHelp = $True

    [void] $OpenFileDialog.ShowDialog()
    Start-Process powershell.exe -Credential $(Get-Credential) -NoNewWindow -ArgumentList “Start-Process powershell.exe -Verb runAs $($OpenFileDialog.Filename)”

    This presents the user with a popup “select file” window that defaults to their desktop folder and filters for *.ps1 files, then presents the user with a popup credentials window, and finally executes the specified script using the supplied credentials.

  11. DISAM says:

    Hello THanks for your post. The Administrator powershell windows was opened after running the cmdlets

    (Start-Process powershell.exe -Credential $(Get-Credential) -NoNewWindow -ArgumentList “Start-Process powershell.exe -Verb runAs $($OpenFileDialog.Filename)”) ,

    after entering the username & password i got the administrator poershell window. May I know how can I run a command with the cmdlet , like this

    Start-Process powershell.exe -Credential $(Get-Credential) -NoNewWindow -ArgumentList “Start-Process powershell.exe -Verb runAs -argument Enable-ComputerRestore -Drive “C:” “

    but is’s getting this error like this:

    Start-Process : A positional parameter cannot be found that accepts argument ‘C: ‘.
    At line:1 char:14
    + Start-Process < <<< powershell.exe -Credential $(Get-Credential) -NoNewWindow -ArgumentList "Start-Process p
    .exe -Verb runAs -argument Enable-ComputerRestore -Drive “C:” “
    + CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

    please help me. Any help would be greatly appreciated.

    Thank You.

  12. DISAM says:

    Hello, I found a solution that has been mentioned all the steps how to run the powershell from cmd and so..on, here is the link, this may be helpful to you, Link: http://madurad.wordpress.com/2014/03/19/how-to-enable-system-protection-on-all-user-machines-on-a-domain/

  13. Anonymous says:

    L’un de mes clients a récemment soulevé le fait qu’il ne pouvait pas exécuter un

  14. Anonymous says:

    I too get the same error A positional parameter cannot be found that accepts argument ‘C: ‘.

Comments are closed.

Skip to main content