Running a Script to open a program as administrator

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of H Man H Man 11 months, 4 weeks ago.

  • Author
  • #31562
    Profile photo of Moore Family
    Moore Family

    Hi, I was wondering if it is possible to create a powershell script that uses the start-process command allowing me to open up a program (lets say cmd) as the administrator?
    The reason is I want the 'do you want to make changes to your pc....' sign to come up, in order for me to try use send keys to type in the password.

    I would appreciate some help as I am new to powershell. thanks.

  • #31567
    Profile photo of Peter Jurgens
    Peter Jurgens

    Start-Process accepts a -Credential parameter which you can use to specify alternate credentials. This will help run the executable with credentials that will allow elevation, however you'll still be prompted to elevate.

    If you wish to immediately elevate, use the -Verb parameter with 'RunAs' as the value and that will automatically prompt you to run the process with an admin token. Credential and RunAs however are part of different parametersets therefore they cannot be run together.

    I'm not 100% clear on what it is exactly you're trying to do though? Could you elaborate?

  • #31569
    Profile photo of H Man
    H Man

    try this

    $Username = 'Username'
    $Password = 'Password'
    $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
    $Credential = New-Object System.Management.Automation.PSCredential ($Username,$SecurePassword)
    Start-Process powershell.exe  -Credential $Credential -NoNewWindow -ArgumentList "(Start-Process -FilePath '\\some\path\app.exe' -ArgumentList  '/q).ExitCode"


You must be logged in to reply to this topic.