5

#PSTip Start PowerShell as a different user with elevated privileges

Note: This tip requires PowerShell 2.0 or above.

This command is useful when you want to perform some tests or if you work in an environment where you use the standard and admin user accounts. Open Command Prompt and run the following command:

C:\> C:\Windows\System32\runas.exe /env /noprofile /user:CHANGEME@TEST.LOCAL "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -command \"start-process powershell -verb RunAs\""

The best way to use it is to create a shorcut on your desktop and put it in the Target field.

Filed in: Columns, Tips and Tricks Tags: ,

5 Responses to "#PSTip Start PowerShell as a different user with elevated privileges"

  1. Sagun says:

    Aleksandar- can you assist in letting me know what I’m doing wrong in the script below?, I’m trying to run the script logged in as a standard users’ profile with admin credentials, I’ve tried using both a local admin account and my domain user account that has local admin rights, but I get the error listed below. It is just a simple uninstall/wait/install, any help is appreciated, thank you.

    SCRIPT:
    $username = ‘.admin’
    $password = ‘Window”s123’
    $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
    Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq “On-Screen Takeoff”} | foreach-object -process {$_.Uninstall()}
    Start-Sleep -Seconds 25
    $arguments=”/quiet”
    Start-Process “\domain.comROOTInstallsOnCenterOSTTestverion3906ost3906.msi” $arguments

    ERROR:
    [localhost] Connecting to remote server failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and docu
    mentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: “winrm quickconfig”. For more inf
    ormation, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken

    • Aleksandar says:

      Is that your whole script? The error message is typical for PowerShell remoting, and I don’t see in your code that you use Invoke-Command or Enter-PSSession commands. Btw, how is this problem connected to my tip?

      • Sagun says:

        My original code is called part 2 of another script, but I still got the same error. I was just trying to run the script with admin rights and given your tip and your knowledge of powershell, you could point me in the right direction, thanks again.

        PART2
        (Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq “On-Screen Takeoff”} | foreach-object -process {$_.Uninstall()}

        Start-Sleep -Seconds 25
        $arguments=”/quiet”
        Start-Process “\domain.comROOTInstallsOnCenterOSTTestverion3906ost3906.msi” $arguments)

        ORIGINAL SCRIPT:
        $username = ‘.admin’

        $password = ‘Window”s123’

        $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))

        Invoke-Command -FilePath \domain.comROOTInstallsOnCenterOSTTestverion3906part2.ps1 -Credential $cred -ComputerName $env:computername

        • Aleksandar says:

          I’m still not sure that I understand what you want to do.

          $env:computername is your local, not remote, computer.
          -FilePath parameter is used to run the specified local script on one or more remote computers, but you specify your local computer and pass something that looks like a UNC path. What is domain.com in that path?

          • Sagun says:

            All I want to do is run the script and make it work as an administrator in powershell while logged in as a standard user account. The script will work if I log in as a local admin or in my domain account in powershell.
            The the domain.com is really just davisconstruction.com and it’s actually supposed to be \davisconstruction.comROOTInstallsOnCenterOSTTestverion3906part2.ps1 -Credential $cred -ComputerName $env:computername

            thanks again.

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: