Have you ever tried calling PowerShell.exe with the -NoExit argument and wondered why PowerShell is still exiting when it’s done your script? For example, if you want to quickly launch a new clean PowerShell session that immediately runs a script, you might run this:
PowerShell.exe -Command “your script” -NoExit
When you do this, PowerShell still exits after it is done with your script. Also, if you were paying attention it likely reports an error that references “NoExit”. It seems that this happens because PowerShell.exe looks at all arguments after -Command and treats them as the command you want to execute in your new PowerShell session.
The solution to this problem is simple. Always make -Command the last named argument in your argument list. Looking at the above example, that means rearranging the command to look like this instead:
PowerShell.exe -NoExit -Command “your script“
This will execute your script and leave PowerShell open for you to work with it afterwards.
|Share this post:|
3 thoughts on “PowerShell Quick Tip: Use the command argument last when calling PowerShell.exe”
[…] For related information, specifically pertaining to the order you need to use in your PowerShell.exe arguments, read this post. […]
This is still happening with `powershell.exe -NoExit -File “./script.ps1″`.
Sorry for the delayed approval, I just found these comments in my spam folder and moved them out.
I’m not sure why you would be having this problem with the two examples you provided. Using Windows 10 with the Anniversary Update (plus Slow Ring updates), your examples work just fine after pressing Win+R and then entering the commands.