Let's say I run a node server via node server.js
In PowerShell (sans ISE) I can hit CTRL + C or CTRL + BREAK twice to stop the server. However, running the same shortcut in ISE will make it hang. It says "stopping" in the bar across the bottom (I imagine this would be referred to as the status bar) but never actually stops.
Is there a shortcut that I'm unaware of?
One way to handle this is to create a new powershell tab, then kill the server from the new tab (using stop-process for instance). Then switch back to the old tab and (after a possible few second delay) the server will have been terminated. Once you have the command to terminate the server in the command line history of the second PS tab, it will be a little quicker to subsequently kill the server.
Another way I handle this if I have a PS script (vs an external program) that has hung is to create a new powershell tab (ctrl-T), then click the old tab, then click the X on the old tab. Unfortunately you'll have to reopen any files you were editing.
Related
Currently I've got my PowerShell script on the desktop named Chrome with a Chrome logo on it. They launch that, it opens chrome for them to use but if you tab out you'll see a PowerShell script idle counting until it reaches the terminate period.
Issue - Small business owner that has several computers employees use for web (chrome) based duties day to day. Employees often leave without logging out and closing their browser.
My Solution - Run chrome in incognito by default
While this works, they are now leaving their incognito tab open and not closing it to prevent logging out...
My Solution - deployed powershell script to close chrome after a set period of idle mouse and KB.
WHY I'M HERE - Is there any way to prevent them from closing out of my powershell script? Also is there a way to make it less obvious a giant powershell icon in the dock?
This answer provides an overview of launching applications hidden.
A non-third-party solution that requires a helper VBScript, however, is described in this answer.
Assuming you have created such a script and named it runHidden.vbs and placed it in C:\path\to\HelperVBScript, you can create your shortcut file with a command line such as the following:
wscript.exe C:\path\to\HelperVBScript\runHidden.vbs powershell.exe -file c:\path\to\your\script.ps1
This will launch your PowerShell script (.ps1) invisibly, while allowing it to launch GUI applications such as Chrome visible - and only the latter will appear in the taskbar.
My first thought would be to have it run as a job when the computer is logged in. Look into launching your script as a scheduled task with the command Start-Job.
Example:
Start-Job -ScriptBlock {### YOUR CODE HERE ###}
Then, you'd need to create a scheduled task to launch this script at login.
Here's a guide on how to do that: https://blog.netwrix.com/2018/07/03/how-to-automate-powershell-scripts-with-task-scheduler/
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
When Executable writes to console (using the -console option), if I press "enter" a second time, it bring me back to the command prompt even though Executable is still running in the background. How we can force it to output everything to the console and only when done should it return to command prompt?
The installer on windows is a GUI executable, it cannot make the console wait for the process to finish. There are two ways around this:
1) Select the "Windows console executable" property of the installer on the Installer->Screens & Actions step. Drawback: When starting the installer from the explorer, a console will be opened.
2) Start the installer on the command line like this:
cmd /c installer.exe -c
When using the CMD prompt, you can "type ahead" commands and when the current operation is finished, the command will be issued to the CMD prompt.
When you execute the following in CMD, foo will run, then bar will run. However in PowerShell, it will ignore bar.exe<ENTER> because foo is running. Is there a way around this? It's pretty frustrating to have to wait for a command to finish before executing a subsequent command.
foo.exe<ENTER> (takes 60 secs)
bar.exe<ENTER>
It depends on the host. In the console host (powershell.exe) it works the same as cmd (the way you want it to). In ISE (powershell_ise.exe) it does not.
You won't be able to change this behavior.
Your workaround then is to use the console host instead of ISE for interactively typing commands.
If you know you're going to run both, write foo.exe; bar.exe
Or write them both in the ISE edit window, and press F5 to run.
I am new to server core installations so to understand them better I created a VM through Hyper V, installed Windows Server 2012 as Server Core. Accessed the computer and kept trying to run the Netdom.exe RenameComputer function.
I switched over to my main server and then switched back and I have lost the Command Line prompt.
How do I get this back? Cntrl+Alt+Del do not seem to have an option to launch it?
Press CTRL+ALT+DELETE, click Start Task Manager, click File, click Run, and then type cmd.exe. Alternatively, you can log off and log back on.
Press Ctrl+Shift+Escape to run Task Manager. Choose Expand Details, then File | Run | cmd.exe.
Or File | Run | powershell.exe if you want PowerShell instead of Command Prompt (which you should :-).