共用方式為


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 -NoNewWindowparameter 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

  • Anonymous
    January 01, 2003
    @ Brian EhlertFor me your suggestion does not work.Do you have a working example?Thanks

  • Anonymous
    January 01, 2003
    How do I run the commands without the "-Credential" option with admin privileges. Such as get-eventlog or get-childitem

  • Anonymous
    November 15, 2012
    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

  • Anonymous
    February 05, 2013
    By removing -Credential, Start-Process will not pop a new window.

  • Anonymous
    February 28, 2013
    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"

  • Anonymous
    April 05, 2013
    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))

  • Anonymous
    September 25, 2013
    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.

  • Anonymous
    November 07, 2013
    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.

  • Anonymous
    January 28, 2014
    The comment has been removed

  • Anonymous
    March 04, 2014
    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.

  • Anonymous
    March 06, 2014
    The comment has been removed

  • Anonymous
    March 19, 2014
    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/

  • Anonymous
    January 20, 2015
    The comment has been removed

  • Anonymous
    February 03, 2016
    I too get the same error A positional parameter cannot be found that accepts argument 'C: '.