How would I write the following windows command in powershell?
start /b /min FeedDemon.exe
When I run that on the command line, it minimizes the window fine and works great in a startup script. But in powershell, I cannot get the same results when I try:
start-process -FilePath FeedDemon.exe -WindowStyle Minimized
I am still learning powershell (and Windows in general) so please be gentle.
The /b argument to start indicates that the program should run without popping a new console window. The direct translation to powershell would be
Start-Process FeedDemon.exe -NoNewWindow -WindowStyle Minimized
If that doesn't work, just call it the old fashioned way, from Powershell
cmd.exe /c start /b /min FeedDemon.exe
Related
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.
I have a batch script being that has the following code in it:
Here is the batch script (Test.bat):
call powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1
exit /b
The Powershell script (TheScriptToBeExecuted.ps1) has some wait built into it. But when this file is being called, the batch script is NOT waiting for the Powershell script to finish.
I ever tried:
START /wait powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1
To no effect.
When I double click on the Test.bat file, it seems to be waiting. I have checked out a lot of answers, but couldnt find any that correspond to this issue.
Please help.
start /wait powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1 | ECHO > nul
Try the above. Holds the command until scripts executed, without the Teminate Batch Prompt. Use start /min /wait if not needing to display or interact with the script, or Alternately:
start /wait powershell.exe -executionpolicy remotesigned -WindowStyle Hidden -File TheScriptToBeExecuted.ps1 | ECHO > nul
-WindowStyle Value
Sets the window style for the session. Valid values are Normal, Minimized, Maximized and Hidden.
* | * pipes the output of the command, however as your just starting powershell.exe and not using it in the context of utilizing a command like follows, It shouldn't be an issue.
powershell -command "((Get-date).AddDays(0)).ToString(':dd:MM:yyyy') | set-content 'captureVar.txt'" && set /p Yesterday=<captureVar.txt
It worked for the script I tested, but it's not thoroughly tested.
let me know how it goes.
I think the issue is more likely to be within "TheScriptToBeExecuted.ps1" than your bat. In fact several powershell command are working as Asyncronous.
I tested a Sleep-Start, which is not Asyncronous at all using the following code and the bat is waiting the end of it.
This is the code I used in my .bat
REM RUNTEST.BAT
time /T
echo started
powershell -executionpolicy remotesigned -File C:\Users\Alex\Desktop\DA_PROVARE\test.ps1
time /T
echo done
exit /b
The ps1 content is just one row, here below:
Start-Sleep -Seconds 25
The problem you are facing is probably because of the account under which the scheduler runs. Have faced similar problems where the .bat and/or .ps1 doesn't behave the same if launched by a scheduler or other tools
powershell -ExecutionPolicy ByPass -command ". 'C:\path\TheScriptToBeExecuted.ps1'"
This should do the it, if not then we will add < NUL to the end
powershell -ExecutionPolicy ByPass -command ". 'C:\path\TheScriptToBeExecuted.ps1'" < NUL
By using dot sourcing, we are opening powershell in the cmd window with the powershell reference in the above solution. Then the period allows the script to run in cmd window also. I did a double test and made a .ps1 with start-sleep -s 3 in it, and the above did wait for the powershell script to finish.
method 2
Using Start /wait
START /wait PowerShell.exe "& "'C:\path\TheScriptToBeExecuted.ps1'"
This method will not run inside the cmd window. It opens a powershell terminal and runs there then goes back to the bat.
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.
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
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"