PowerShell Quick Tip: Use the command argument last when calling PowerShell.exe

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.

Kirk out.

Share this post:

Advertisement

3 thoughts on “PowerShell Quick Tip: Use the command argument last when calling PowerShell.exe

    1. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s