Starting an powerscript with start-process command

D.P.J. van der Gragt 0 Reputation points
2023-06-23T06:11:24.6333333+00:00

I try to start an powershell script with adminright from an other powershell script.

i have a small problem with starting an powershell script from another powershell script.

what's working:

I can start powershell with admin rights. that's not an issue. but what i want to do is:

start-process powershell -verb runas -argumentlist "-file .\scriptname.ps1"

what happens is powershell opens, en closes directly

when i use within powershell .\scriptname.ps1, the script is running without problems.

hope someone can help me to figure out why it's not working

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2023-06-23T11:04:44.2633333+00:00

    You need to fully qualify the path to the script because runas will set the current working directory to C:\WINDOWS\system32.

    Add the noexit switch so that PS does not terminate immediately, and you can see the error message.

    start-process powershell -verb runas -argumentlist "-noexit -file C:\MyScripts\scriptname.ps1"
    
    
    
    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.