I would like to run new powershell window with parameters. I was trying to run following:
powershell -Command "get-date"
but everything happens in same console. Is there a simple way to do this?
To open a new PowerShell Window from PowerShell:
Start-Process PowerShell
Or my personal favorite:
Start-Process PowerShell -WindowStyle Maximized
Then you could typeGet-Datewithout having to deal with the -ArgumentList's tendency to close itself. I still haven't found a way to open a new PowerShell process with the -ArgumentList Parameter, without it immediately closing after it runs. For Instance:
Start-Process PowerShell -ArgumentList "Get-Date"
or simply
Start-Process PowerShell -ArgumentList Get-Date
Will Close Immediately after running the process.
In order to get it to wait before closing you could add:
Start-Process PowerShell -ArgumentList 'Get-Date; Read-Host "Press Enter"'
Since the -Wait Parameter seems to do nothing at all in this case.
FYI - PowerShell Suggested Syntax is actually:
Start-Process -FilePath "powershell.exe"
But since PowerShell is a standard Windows Application in the %SystemRoot%\system32 Environment Variables the command line(s) should recognize a simple
Powershell
Command
Use the start command. In a CMD prompt, try:
start powershell -noexit -command "get-date"
For Start/Run (or Win+r) prompt, try:
cmd /c start powershell -noexit -command "get-date"
The -noexit will tell Powershell to, well, not to exit. If you omit this parameter, the command will be executed and you are likely to just see a glimpse of a Powershell window. For interactive use, this is a must. For scripts it is not needed.
Edit:
start is an internal command for CMD. In Powershell it is an alias for Start-Process. These are not the same thing.
As for why the window is black, that's because the shortcut for Powershell.exe is configured to set the background blue.
To call a PowerShell (PS) script in a second terminal window without exiting, you can use a script similar to:
Start-Process PowerShell -ArgumentList "-noexit", "get-date"
or if you need to run another script from a specific location:
Start-Process PowerShell -ArgumentList "-noexit", "-command .\local_path\start_server.ps1"
Related
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.
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
I'm running a powershell script from a .bat to make it double click friendly. Here's the code:
powershell.exe -ExecutionPolicy unrestricted -NoExit -command "Start-Process powershell -Verb RunAs" {<Location of .ps1 file here>; <arguments>; pause }
It runs fine but when it completes it says "Press any key to continue" and the powershell window closes but I need it to remain open because at the end of the script it displays some additional commands we need to run to complete the process.
Any ideas? Thanks!
To keep a PowerShell window open (its session alive) after executing a command with -Command (or -File), use the CLI's -NoExit parameter, as already shown in your question for the outer PowerShell call.
You then have no need for an interactive delay-the-closing command such as pause (which is a function that simply calls $null = Read-Host 'Press Enter to continue...').
Therefore:
Add a -NoExit parameter to the inner, Start-Process-launched PowerShell instance as well or - more likely - instead; you need to scroll to the right to see it in the command below.
Remove the pause command from the inner command string.
Note: I'm assuming that you don't actually need the outer -NoExit, as it would block the batch file indefinitely by entering an interactive PowerShell session, so it is omitted from the command below.
powershell.exe -ExecutionPolicy unrestricted -command "Start-Process powershell -Verb RunAs '-NoExit', '-Command', '...'"
Note that there's no point in using a script block ({ ... }) with Start-Process, because only strings are supported as arguments by Start-Process; hence, '...' is used above to represent the command string to pass to the inner PowerShell instance.
I am trying to run the following PowerShell command in CMD:
powershell -Command "Start-Process MSBuild.exe MyProject.sln -Verb RunAs"
I'm running this in PowerShell so that I can get the UAC (for elevated privileges). I'm not sure if there is an equivalent in CMD.
Now, I run the PowerShell script from within a batch file, so that I can double-click and execute. (or put it in the $Path location and call it from anywhere)
But the problem is as soon as it finishes running, it immediately closes, and I cannot see the build error message if any.
How can I wait or pause when MSBuild.exe has finished executing in a new window?
The noexit command keeps your PowerShell window open.
powershell -noexit -Command "Start-Process MSBuild.exe MyProject.sln -Verb RunAs"
in your batch file, add pause to the end
powershell -Command "Start-Process MSBuild.exe MyProject.sln -Verb RunAs"
pause
The issue you're seeing is the command window closing after executing everything in the batch file, rather than powershell closing the shell after it executes.
Need to launch several jobs in PowerShell, but they should be in different sessions. So to launch one, one could use:
Start-Process powershell -ArgumentList "-command &{Get-Process}","-noexit","-noprofile"
But the new window closes as soon as the command finishes. Even though I'm using -NoExit parameter. Acording to this article and this question, this should work. Even tried to block the window, by waiting for an user input, but it just closes.
While the command parameters are named, position is still critical (see PowerShell.exe /?):
Start-Process powershell -ArgumentList "-noexit", "-noprofile", "-command &{Get-Process}"
I can run this fine:
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"
start-process $msbuild -wait
But when I run this code (below) I get an error:
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /v:q /nologo"
start-process $msbuild -wait
Is there a way I can pass parameters to MSBuild using start-process? I'm open to not using start-process, the only reason I used it was I needed to have the "command" as a variable.
When I have
C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe /v:q /nologo
on a line by itself, how does that get handled in Powershell?
Should I be using some kind of eval() kind of function instead?
you are going to want to separate your arguments into separate parameter
$msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"
$arguments = "/v:q /nologo"
start-process $msbuild $arguments
Using explicit parameters, it would be:
$msbuild = 'C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe'
start-Process -FilePath $msbuild -ArgumentList '/v:q','/nologo'
EDIT: quotes.
Warning
If you run PowerShell from a cmd.exe window created by Powershell, the 2nd instance no longer waits for jobs to complete.
cmd> PowerShell
PS> Start-Process cmd.exe -Wait
Now from the new cmd window, run PowerShell again and within it start a 2nd cmd window:
cmd2> PowerShell
PS> Start-Process cmd.exe -Wait
PS>
The 2nd instance of PowerShell no longer honors the -Wait request and ALL background process/jobs return 'Completed' status even thou they are still running !
I discovered this when my C# Explorer program is used to open a cmd.exe window and PS is run from that window, it also ignores the -Wait request.
It appears that any PowerShell which is a 'win32 job' of cmd.exe fails to honor the wait request.
I ran into this with PowerShell version 3.0 on windows 7/x64
I've found using cmd works well as an alternative, especially when you need to pipe the output from the called application (espeically when it doesn't have built in logging, unlike msbuild)
cmd /C "$msbuild $args" >> $outputfile
Unless the OP is using PowerShell Community Extensions which does provide a Start-Process cmdlet along with a bunch of others. If this the case then Glennular's solution works a treat since it matches the positional parameters of pscx\start-process : -path (position 1) -arguments (positon 2).