I am running a program which calls powershell via the system command multiple times. At the end I end up with many opened windows. I would like either these windows to close after each iteration has been completed or run in the background without opening the command window.
pubPath = 'powershell -inputformat none cd C:some path in my pc &';
[status,publisher] = system(pubPath);
I've tried the exit and exit() commands at the end of my paths but nothing has worked so far. Any help would be great.
For future reference, I found a solution to my issue. Execute this command after the loop executes all its instances and all the spawned command windows will be closed. BECAREFUL, you might kill more processes than you want.
system('taskkill /IM cmd.exe')
Related
In Windows PowerShell 5.1, after run & code ., a VSCode window opens, and the control returns back to PowerShell immediately. After the PowerShell exists, the VSCode will not be terminated.
On the other hand, when invoke other external program, such as WinMerge, after run & WinMergeU, a WinMerge window opens, and the control does not return back to PowerShell until WinMerge window is closed. And If PowerShell exists, WinMerge will be terminated.
Why the behaviour is different?
the difference is what is actuall happening:
when you run the command code, you are not really running code.exe. its starting a cmd script that spawns a new code.exe process with whatever arguments you passed it.
to see what a command actually executes, use the command get-command 'yourcommand', or with code get-command code.
this will show the follwing source: C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd.
Opening up this will show you:
#echo off
setlocal
set VSCODE_DEV=
set ELECTRON_RUN_AS_NODE=1
"%~dp0..\Code.exe" "%~dp0..\resources\app\out\cli.js" --ms-enable-electron-run-as-node %*
endlocal
so this means that in both cases you are waiting for execution to end, but for code its a code.cmd script and not actually code.exe.
If you want to start new processes and don't wait for them, you can use the command start-process winmergeu
The problem I am having is that any powershell script that I try to run, either by double clicking or using "powershell /path/to/script" from cmd, will open a window, then close, then open another window, then close, and so on... The code never actually runs though.
I have set the execution policy to what it needs to be to allow powershell scripts to run. I have tried running as administrator. I have tried this on 2 separate computers running Windows 10. I've tried using these 3 scripts as test scripts with the same result:
1
write-host("hello")
2
write-host("hello")
cmd /c pause
3
cmd /c pause
Has anyone seen this behavior and know how to fix it?
I have a command that calls powershell using !powershell . it works fine but, the first command will execute a publishing of data action from an external program and does not proceed to next line of code in MATLAB until there is a subscriber to the published data. the problem is that the next line of code is the one that subscribes to the published data so it just runs for ever waiting for the data. any ideas how to make the code continue? I have tried the continue statement but since I have called powershell, it stays there and MATLAB commands do not get executed. Also, I have tried to run the commands backwards, so subscriber first and publisher after but get same issue. Any ideas?
pubPath = 'powershell -inputformat none cd path' ;
subPath = 'powershell -inputformat none cd path2';
[status_one,publish] = system(pubPath);
[status_two,subscribe] = system(subPath);
You need to start the job in the background, such that PowerShell returns immediately before the job finishes. Note that it is PowerShell that waits for the job to finish, not MATLAB.
End the PowerShell command with an ampersand (&) to run it in the background:
[status_one,publish] = system('powershell -inputformat none cd path &');
I have to run cmd / c from a program, run the start command xx.exe, and I capture the result (there xx.exe?). until everything is right, however, remains open the console with the error popup. how can I close the console with the error?
Usually win32 applications will close the command prompt after execution. If this isn't the case with what you're trying to run, you could:
Run it from Windows "Run" option (Windows button+R) than your program name and path in prompt.
Run it from a batch file, like so:
runMe.bat:
START "" "C:\windows\notepad.exe"
EXIT`
Than just run runMe.bat from wherever. Notice the 'exit' command that closes the command prompt after execution.
Read more about batch files, the start command, and this issue here, and there.
Good luck!
#echo off
start /wait notepad
start worpad
This is the code i have written in a batch file. My aim is to stop the batch file execution till the notepad application gets closed. Its working perfect but the thing is, Its displaying the command prompt also .Its opening the command prompt when i execute
start /wait notepad in my batch file.
The command prompt gets closed when i close my notepad. But i dont want the command prompt.How do i make that. I even tried these
cmd /c start /wait notepad
even the above command is not working. How do i make it.How do i open only notepad without the command prompt and wait till it is closed ?
As I said in my answer to one of your previous questions, the command prompt window is there because it is the tool that processes the batch file. The command prompt window is the working window of the CMD.EXE program, just like Notepad's working window is the one where you are editing text files. Typically, running a program with its working window hidden is a non-trivial task, unless the program has a pre-defined mode of running with the hidden window. As it happens, CMD does not have such a mode.
However, there is a way of starting a program with its window minimised. You only need to create a shortcut to your program (it can be a batch file too), then open the shortcut's properties, and on the Shortcut tab, set the Run property to Minimized. To make it clearer, here's an illustration:
Or maybe you can just use the
ping localhost -n ( your time in second ) >nul
So your code will be like this
#echo off
start notepad
ping localhost -n ( your time in second ) >nul
start worpad