PowerShell script to open a new PowerShell window and run command - powershell

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

Related

Powershell scriptopen a window and send command to window

I'm trying to open a CMD as system using PowerShell and then send a command to that system window.
what i've got so far is this:
$opensystempromt = '"/c PsExec.exe -is cmd.exe'
[System.Diagnostics.Process]::Start('cmd.exe',$opensystempromt)
$systemcommand = 'CustomerService.exe deploy aa.txt bb.txt'
what i can't figure out is how to send the system command to the newly opened CMD.
and there's no documentation for it anywhere, that i could find
I'd use Start-Process to open cmd.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -verb runas -ArgumentList {/k Insert your variables here}
with -verb runas you'll open cmd as an administrator. ArgumentList is your "command part". /k leaves the cmd open, after the commands are done. If you want cmd to close instead, use /c.

How to run PowerShell Start-Process without closing the output window?

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.

Start PowerShell with -NoExit not working

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

How to call .cmd file as administrator?

Please let me know how to call .cmd file as administrator from PowerShell script:
The second line below should open as Administrator from a PowerShell script:
Set-Location "C:\client\service"
Invoke-Item "C:\client\service\_install.cmd"
Then the command prompt should wait after execution. This needs to handle in PowerShell script not possible to write in _install.cmd file.
Batch-scripts runs in CMD.exe, so you need to start a CMD.exe process as admin.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -ArgumentList "/k","C:\client\service\_install.cmd" -Verb RunAs -Wait
Start-Process is the cmdlet to start a process
-FilePath "C:\Windows\System32\cmd.exe" starts cmd.exe process
-ArgumentList "/k","C:\client\service\_install.cmd" tells cmd to leave the console open after running the script (is this what you wanted? if not, replace with /c so the cmd-window will close when done). The second argument is your script.
-Verb RunAs tells Start-Process to start the process as admin (you will recieve a UAC-window if enabled)
-Wait tells Start-Process to wait until the process is finished. With cmd /k this means after you exited the command prompt. If you've changed that to cmd /c, then it waits until the script is done.
If you need to change the working directory inside the cmd-file, then you need to modify the .cmd, or write a wrapper-script, like:
#echo off
cd /d C:\client\service
C:\client\service\_install.cmd

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"