I'm looking for a way to write a script which is able to open a new PowerShell window and to run command. E.g. I'd like to launch PowerShell, in this window I need to run a script able to open a new PowerShell window inside which the command pwd is executed.
1 Answer
Try on of this Commands
Start-Process powershell.exe -ArgumentList "-noexit"
Start-Process powershell -ArgumentList "-noexit", "-noprofile", "-command &{Get-Location}"
to start the Window maximized us the Parameter -WindowStyle Maximized
Provide more Code for further Information
4 Comments
mklement0
Plus 1 for the need for
-noexit to keep the new window open (only needed if -Command / -c are also specified), but two asides:mklement0
While passing arguments as an array to
-ArgumentList conceptually the best approach, it is unfortunately ill-advised due to a long-standing bug in Start-Process, still present as of this writing (v7.1) - see GitHub issue #5576. For now, using a single string comprising all arguments, enclosed in embedded "..." quoting as necessary, is the only robust approach. As discussed in the linked GitHub issue, an -ArgumentArray parameter that supports robust array-based argument passing may be introduced in the future.mklement0
Note that there's no reason to use
& { ... } in order to invoke code passed to PowerShell's CLI via the -command (-c) parameter - just use ... directly. Older versions of the CLI documentation erroneously suggested that & { ... } is required, but this has since been corrected.mklement0
*Correction to the first comment:
-noexit is only needed if one of the following parameters are also specified: -Command (-c), -EncodeCommand, -File.
Start-Processcmdlet - on Windows, it runs the specified executable in a new window by default (on Unix, the command invariably runs in the same window).Start-Process powershell -ArgumentList '-NoExit "& {pwd}"'Don't forget to check helppowershell /?, Get-Help Start-Process