Starting PS Script as administrator in CMD

Sorry first
english is not my nativ language, and i am an normal user of Powershell.

so my question i want start an Powershell script as administrator from cmd.

i using follow command in my batch-file:

PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File "test.ps1"' -Verb RunAs}"

Olny powershell Windows flashing but nothing happend

can someone help me to fiy my problem ?

@MCS welcome to PowerShell.org community.

Its probably executing the test script and exiting. What are you doing in the test.ps1

The account running the CMD has to be an administrator to be able to elevate the PowerShell process. You know that, don’t you? :wink:

Its olny a test , that the ps1-script make a break and wait press button

Script Test.ps1
write-Host “pause”
Pause

And yes is have to start as administrator

Pause is an internal command from CMD. You cannot use it in PowerShell without actually calling CMD. :wink:

You can use somthing like this instead:

Write-Host -NoNewLine 'Press any key to continue...'
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')

it should work actually, its a function in PowerShell which calls Read-host internally. Calling Read-Host alone should also work.

c:\> ${function:pause}
1 Like

I change like you say, but nothing change

ist olny flashing the powershell window :frowning:

I think there is any problem with the execution policy. but i cnt find the failure

… learned something new again today … thanks. :+1:t4: :wink:

1 Like

You could try to add the parameter -NoExit to the command line …