Start PowerShell with -NoExit not working - powershell

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}"

Related

Running cmd.exe from Powershell and passing another exe application as a Parameter

Ok so I have been looking for an example on how to do the following but can't seems to find the correct answer. I have found a lot of things that are close but won't work. This code is being run from within a PowerShell script if matters or makes a difference.
Here is the code snip:
Start-Process -FilePath cmd.exe -ArgumentList /k "msdgen170.exe" -Wait -WindowStyle Maximized -Verb RunAs -WorkingDirectory "C:\Program Files (x86)\3E\msdgen"
It works as far starting the cmd window but I can't seem to get the msdgen170.exe application to be passed to the window. It opens and stops at a prompt. I have tried the & but that doesn't work either.
What am I missing?
Thanks for helping to point me in the correct direction.

PowerShell script to open a new PowerShell window and run command

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

Supress PowerShell prompting "Press any key to continue..."

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.

Call a powershell script from another powershell script as local admin

not sure if I'm overcomplicating this, I just want to call another script as local admin and have it run.
The command below opens powershell, and in the new window I get an error and then the window closes too fast for me to be able to see it.
start powershell -verb runas -ArgumentList {Invoke-Expression -Command "C:\Script.ps1"}
If you want to stay native powershell use the start-process cmdlet and you can specify the filepath (process to run) as powershell.exe and the -ArgumentList parameters as conditions for your new session. In my example below i'm setting ExecutionPolicy so you don't have to rely on the system level policy, NoProfile will make your script a bit more resiilent by not loading any customized profile on a system.
$Cred = (Get-Credential)
$ScriptLocation = "C:\Temp\TestScript.ps1"
Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File $ScriptLocation" -Credential $Cred
You can see in the script getting a credential object (which you'll probably want to provide so the script will just run) then specifying the script location and executing.
As vonPryz mentioned, you can always troubleshoot by adding -NoExit to your Argument list so the window stays open after executing the script but keep in mind if that if the script location doesn't exist you'll still see the powershell host appear and close right away.
You can also add -WindowStyle Hidden to your argument list to hide any window from appearing at all.
Turned out to be an execution policy issue. This was a device that was non-standard in our environment and didn't have that GPO pushed.

Run powershell in new window

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"