I set the path to a program, say "foo.exe", to my system path and so typing foo in cmd/powershell starts the program. However when I type exit to get out of cmd/powershell foo.exe also closes with it. Why does this happen and how do I prevent this from happening?
This doesn't happen for all programs, only certain ones which means those certain ones should be added to path in a different way probably or should be started in a different way I'm guessing. However, searching over the internet for a long time didn't give me anything so a little help would be appreciated.
If foo.exe is a console application (one compiled for the Windows console subsystem), it will run synchronously in cmd.exe / PowerShell: that is, control won't be returned to the calling shell until the application exits. This means that you won't even get a chance to type exit until foo.exe has already exited.
However, it is possible to run a console application asynchronously, namely if you use a job to run it, via Start-Job or Start-ThreadJob; that is, foo.exe will then run in the background.
In that event, exiting the calling shell with exit will terminate the foo.exe process.
To prevent that, you can use the Start-Process cmdlet instead; on Windows, you can use it to launch foo.exe directly, which will open in a new console window by default; on Unix-like platforms, you must launch it via the nohup utility (which sends the program's output to a file named nohup.out in the current directory).
By contrast, if foo.exe is a GUI-subsystem application, it launches asynchronously and independently of the calling shell: that is, control returns to the calling shell right after successful creation of the new process, and exiting the shell has no effect on that new process.
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
Let's say I've been using Powershell ISE for a while and my environment space is starting to get dirty, and I need to restart the interactive shell... I don't want to close my editor and reopen it. How to restart powershell ISE interactive shell to clear all variables without closing and reopening the Powershell ISE?
First, the obligatory notice:
The PowerShell ISE is no longer actively developed and there are reasons not to use it (bottom section), notably not being able to run PowerShell (Core) 6+.
The actively developed editor that offers the best PowerShell development experience, across PowerShell editions and platforms, is Visual Studio Code, combined with its PowerShell extension.
ISE:
Colin's helpful answer is a pragmatic solution: open a new tab and close the old one.
However, that new session invariably retains the environment variables of the old one, because the ISE hosts the PowerShell SDK in-process rather than running powershell.exe as a child process.
To restart a session in the current tab you would therefore have to instruct the hosted System.Management.Automation.PowerShell instance to discard its current runspace and create a new one - and I'm not aware of a way to do this.
Even if it were possible, however, the environment variables - which exist at the level of the process that runs the ISE - would be retained.
Visual Studio Code:
It is possible to start a new session in the current tab and to do so without inheriting the old session's environment variables:
While the integrated terminal running is running the PowerShell Integrated Console, which the PowerShell extension comes with - which is the equivalent of the console pane in the ISE - you can kill the current instance by clicking the trash-can icon in the toolbar of the terminal panel as shown below.
After doing so, you're prompted for starting a new session, which runs in a new powershell.exe / pwsh child process.
Alternatively - and preferably - you can configure the PowerShell extension to automatically start a new session whenever you start a new debugging session, as zett42 points out:
Either: Open the Settings (Ctrl-,) view, search for powershell temporary and turn on the PowerShell > Debugging: Create Temporary Integrated Console setting.
Or: Add "powershell.debugging.createTemporaryIntegratedConsole": true directly to your settings.json file.
This automatically starts a new, temporary PowerShell Integrated Console for each debugging session, which remains open until you start the next debugging session, at which point a new temporary console simply replaces the old one.
Curiously, as of extension version 2022.11.0, you cannot exit out of a PowerShell Integrated Console, but you can use the trash-can icon or Stop-Process -Id $PID to kill it, if needed, which in the case of a temporary console will (commendably) not prompt you to restart it; instead, the next debugging session will create a new, temporary console on demand.
This configuration avoids a major pitfall that afflicts the ISE invariably (and may in part be what prompted the question) as well as the PowerShell extension's default configuration:
There, the code runs dot-sourced, i.e. directly in the top-level scope of the same session, so that the state left behind by earlier debugging runs can interfere with subsequent debugging runs; for instance, create a script with content (++$i) and run it repeatedly - you'll see that $i increments every time, across runs.
Starting a new session for every debugging run avoids this problem.
Ctrl+t opens a new powershell tab that starts as if it was a fresh powershell session.
Try either 1 of below to clear variable memory , it shall help
exit # Exit will quit from Powershell.
Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0 # this will kill all the memory on current session
I have a small problem with my Powershell script.
What do i want:
open CMD via PSEXEC on a remote machine, and work in this "remoteshell" as if i am in front of this machine. There is a commandline tool which i'd like to run, which requires user input after its started (it runs a own "shell")
What the problem is:
i utilize PSEXEC 2.11 with the following command
.\PSEXEC \\$Global:Endpoint -s -accepteula cmd
Cmd gets started as expected.
When i enter the name of the tool (lets call it tool.exe) the inital screen is loaded. But when the shell appears, nothing happens. I can enter commands but there will be no feedback...
C:\windows\System32>tool.exe
Testing Tool V1.0
Command? >
Then nothing can be entered anymore...
I even cannot stop it by pressing CTRL+C. I need to close the application window to force it to close. :(
Any ideas? Are there programs which cannot run in "user interaction" mode?
For reasons, it cannot run as a window visible on the remote endpoint. It needs to run silent.
I have a startup task for my webrole that download some executable file from a blob and then proceed to the installation.
From a .cmd file, I start a power shell script that download the files, then I start the file from the .cmd.
The script works fine if I run it manually through RDP after the publishing is done.
But, when running as startup script, it sometimes (often) fail at different points.
The taskType is set to background.
Last time, the error was that the command PowerShell does not exists...
Also, I use powershell -command set-executionpolicy unrestricted before running my PS script, but I read here that other task may reset this setting and make mine fail.
Quite a mess.
So that makes me think that if I could wait for all other task to perform before starting mine, it would eliminate these kinds of problems
I suppose I could check if some process is running and wait for it to finish, but I have no clue wich process to check.
Or maybe there's another solution.
~edit~
I read here that the error about powershell not existing may be caused by the batch file being saved as UTF-8 in visual studio. I re-writed it from scratch in notepad++ and made sure it is save as ANSI. Then, same error. The full message is :
'PowerShell' is not recognized as an internal or external command,
operable program or batch file.
Again, the script run perfectly from command line in remote desktop.
It would be possible to set an environment variable at the end of the script that is required to finish, then in the script which is awaiting the dependencies, loop until the environment variable is set, then kick off its activities.
You could also run everything from a single powershell script and use the '-asjob' switch on your installer statement, use the 'wait-job' cmdlet to block until the task is complete then carry on. Powershell also offers a '?!' operator which ensures the last statement executed properly.
This might be caused by an encoding issue. As mentioned in this answer you should save your file in ASCII to ensure correct interpretation of your script.
From the linked answer:
Open your whatever.cmd file with your VS 2012 Ultimate. Click on File->Save whatever.cmd as -> on the dialog there is little arrow next to the [save] button. It will show up a menu that will have the option Save with Encoding.
Select it. Now choose "US-ASCII Codepage 20127" from the list of available encodings.
In vbscript, how do I run a batch file or command, in the current cmd prompt window,
without starting a new process.
For example. According to script56.chm (the vbscript help apparently)
Windows Script Host
Run Method (Windows Script Host)
"Runs a program in a new process"
So if I have code that uses that e.g. a VBS file, and a BAT file.
An environment variable g has the value abc g=abc
from that command window,
The VBS file calls the BAT file with windows scripting host Run.
The bat process sets g=z. and finishes.. and the vbs process finishes.
The environment variable is left untouched as g=abc.
I know
CreateObject("Wscript.Shell").Run "c:\test.bat", 0
starts a new window as is clear when using 1 instead of 0. (since 0 hides the window)
How do I
-run the bat file from the vbs, in the same cmd environment that the vbs was called in, so changes affect the cmd environment it was called in?
-In the two windows case which this one is at the moment, how do I access the environment of the parent cmd window, from the batch file?
how do I run a batch file or command, in the current cmd prompt window, without starting a new process?
I don't think you can; your vbscript runs under a script host engine (such as cscript.exe or wscript.exe), and batch files are interpreted by the command interpreter (typically cmd.exe). Both are separate executables and neither is, to my knowledge, available as an in-process library, so you cannot interpret .vbs and .cmd files within the same process. I also highly doubt that the script host engine that is running your VBScript also could run the batch file in its parent cmd.exe - I don't think you can 'inject' a new batch file into a running cmd.exe.
how do I access the environment of the parent cmd window, from the batch file?
Not just access, but change - MSDN's "Changing Environment Variables" is quite explicit on this: "Altering the environment variables of a child process during process creation is the only way one process can directly change the environment variables of another process. A process can never directly change the environment variables of another process that is not a child of that process." You are trying to change the environment of the parent, not child, process. (I do wonder what 'directly' means in the context of this quote, though).
I would guess that the reason for this is security; imagine the havoc that could be wreaked if arbitrary processes could (maliciously or accidentally) change the PATH (or COMSPEC) environment variable of a running process, such as your vbscript host engine process - it could fail to launch your bat file entirely, breaking your program.
It would seem that you're out of luck - however, there are lots of other mechanisms for passing information between processes. Here are a couple of suggestions that are fairly simple to implement when talking between a batch file & vbscript, although it's by no means exhaustive:
Exit codes
Writing to & Parsing the consoleoutput (stdout) or a temp file
If you absolutely need to set environment variables in the parent cmd.exe (and also absolutely need the intermediate step of a vbscript), then you may have to write a wrapper batch file which runs the vbscript, consumes information produced by it and then sets environment variables; because the wrapper cmd is executing in the top-level cmd process, it will be able to change the env vars there.
Footnote: Note that you can change the permanent system/user environment variables (as opposed to process environment variables) from within a VBScript, but I wouldn't recommend this if you are trying to create a transient state; besides this won't affect already-running processes (like the parent cmd.exe) anyway.