I have a Master script that has several options. When you select 1 in the menu, action 1 will be executed and afterwards you\'ll get back to the menu. This
Although start powershell
command looks cleaner, it doesn't allow you to do everything that you can do with invoke-expression
. For example, the following opens a new window, changes its title and background color, and leaves the window open:
invoke-expression 'cmd /c start powershell -NoExit -Command { `
cd -path $env:homedrive$env:homepath/Documents/MySillyFolder; `
$host.UI.RawUI.WindowTitle = "A Silly Little Title"; `
color -background "red"; `
}';
It also runs fine from within another powershell script.
If you try to do this using the start powershell {...}
syntax it will give an error on the title-changing line, and won't keep the window open. (I suppose It's possible that there's some obscure syntax hack that will make start powershell
work, but I haven't been able to find one.)
Using cmd to start powershell?
start-process powershell -ArgumentList '-noexit -command 'Commands for the new PowerShell''
To launch in an external PS window, you can use the following:
invoke-expression 'cmd /c start powershell -Command { [script block here] }'
E.g.:
invoke-expression 'cmd /c start powershell -Command { write-host "Hi, new window!"; set-location "C:\"; get-childitem ; sleep 3}'
Instead of starting a cmd to start a new powershell instance you can:
start powershell {echo hello}
To prevent immediate exit of new started powershell:
start powershell {echo hello; Read-Host}