Home » PowerShell » PowerShell NoExit – Keep PowerShell Console Open

PowerShell NoExit – Keep PowerShell Console Open

PowerShell NoExit switch prevents the PowerShell console window from closing after running the script.

When you double click on the PowerShell script file, or run with the PowerShell option, the script will execute quickly and then disappear.

It doesn’t give enough time or wait for the user to read the output on the console or check for any error that exists on the terminal and close quickly.

In this article, we will discuss the best ways to keep the PowerShell console open after the script finishes its execution.

Let’s practice!

Using PowerShell NoExit Switch

PowerShell -NoExit switch prevents the console window from closing after the script finishes running.

Syntax

PowerShell -NoExit -ScriptPath

For example, let’s consider the script file getservice.ps1 to get a list of windows services.

Get-Service

If you double click or run with PowerShell, it will quickly display a list of windows services and close the console.

To keep the PowerShell console open to read the service status, use the below command in Windows + R

PowerShell -NoExit D:\getservice.ps1

In the above command, we have to use the PowerShell NoExit switch with the script path.

This will keep console windows open until the user closes them.

Cool Tip: How to execute the script with no profile parameter in PowerShell!

Using Read-Host to keep PowerShell console open

Using the PowerShell Read-Host command at the end of the script ensure console windows stay open until you press enter button to end the program.

Get-Service
Read-Host -Prompt "Press Enter to exit"

When you run the getservice.ps1 script as “Run with PowerShell”, will open PowerShell console.

It will display a list of services and display a message as “Press Enter to exit”

Until you press enter button, the PowerShell console stays open.

Cool Tip: How to perform string concatenation in PowerShell!

Conclusion

I hope the above article using the PowerShell NoExit switch to keep the PowerShell console window open after finishing the script is helpful to you.

PowerShell Read-Host cmdlet at the end of the script keeps the PowerShell console open until the user press the enter key.

Cool Tip: How to terminate the script using PowerShell Exit!

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.