Run a powershell script with credentials

Hi guys - I am new here and need some help with my issue.
I need to start a powershell script, where by this script should call an other powershell script.
The specialitiy is, the second script must be started with adeministrator rights.
The name and die PW are known.
My try ware something like this:


$Password=‘test’
$User=‘test’ #the user is an admin
$SecurePW=ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($User,$SecurePW)
$args = “C:\Users\test\Documents\Get_OS_Info.ps1”
Start-Process powershell.exe -Credential $Credential -ArgumentList (“-file $args”) -Wait

The problem: When i start it, the window of powershell will not be stayed oben and do it’work it should.
And, it wasn’t started as the mentioned admin.

What on hell I do the wrong way here?
Thanks in advance for your help.

PS_Scripter,
Welcome to the forum. :wave:t3:

  1. It might be easier to ask same fairly technical questions in your native language. There are some great German forums out there targetting Powershell scripting among other topips … one of them is https://www.mcseboard.de
  1. It sounds a little bit like an XY Problem. What task are you actually trying to do? What info you want to query about the OS?
  1. Why not running both scripts with the proper account / elevated in the first place? I’d try to reduce complexity as much as possible.
  1. Do you mean it does not stay open or it does not stay visible?

  2. Have you tried using the parameter -Verb RunAs to start the second PowerShell process elevated?

  3. Why did you categorize your question as “Open Discussions” and not as “PowerShell Help” if you want to get help for your particular issue?

  4. When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

Guide to Posting Code - Redux <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )