27

In the command prompt, entering PowerShell.exe will start a PowerShell interactive prompt inside of cmd.

How can one exit this and return to cmd.exe? Neither ctrl+c, exit(), nor the break button work.

0

5 Answers 5

24

Ctrl-Break works.

exit() might work, but calling functions with parentheses is not PowerShell syntax, so it's trying to use () as a function parameter, but () is not anything in PowerShell so it's that which generates the error message. exit(1) works, because (1) is a valid expression itself which evaluates to 1 so this is the same as exit 1, and this command sets the %ERRORLEVEL% return value in the cmd environment.

It's described in help about_Language_Keywords:

Exit

Causes Windows PowerShell to exit a script or a Windows PowerShell instance.

When you run 'powershell.exe -File ', you can only set the %ERRORLEVEL% variable to a value other than zero by using the exit statement.

Sign up to request clarification or add additional context in comments.

1 Comment

It somehow doesn't work, if I run PowerShell from inside a *.bat file: PowerShell "ForEach($P in Get-Process steamwebhelper) {$P.ProcessorAffinity=1}; exit" After this command it hangs up. Additionally, I run it from Cygwin terminal and if I don't Ctrl-C fast enough, the terminal freezes.
17

The correct command is exit, this will exit the current PowerShell prompt and return you to cmd.exe.

2 Comments

Can you please tell, after powershell closes ,how can I exit form cmd.exe through batch job. Basically I am writing a batch jon to trucate the data from file.But the scrip is not closing cmd window. @echo off powershell.exe -noexit -Command "Clear-Content -Force E:\Logs\pgbouncer.log; exit %ERRORLEVEL%"
@Madhu add Exit to the bottom of your batch job. So Exit in powershell and Exit in the batch. hth
2

The only way I could get this working inside a ".ps1" file was:

[System.Environment]::Exit(0)

Comments

1

stop-process -Id $PID to close the powershell window completely.

Comments

1

type cmd in powershell command line, eg

PS C:\Users\ziyati\Gitter> cmd Microsoft Windows [Version 10.0.22621.1413] (c) Microsoft Corporation. All rights reserved.

C:\Users\ziyati\Gitter>

that all !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.