If you can start powershell in admin privileges with keyboard shortcut WIN+X>A, how do you exit it. Alt+F4 does not work. I have tried other possible combinations but none seems to work.
Note: I am not asking about Powershell ISE(which i can close with Alt+f4).
I am asking about the on-demand readily available terminal powershell.
To close a PowerShell window using the system menu you can use
Alt + Spacebar, C
Nearly every Windows window has this option. The Close or Exit option may differ based on the context so you'll just have try it and see.
This answer is accurate for the time of its writing but things may change.
Update: If you're using Windows Terminal, no workaround is needed: Alt-F4 works as-is.
The following therefore only applies to regular (conhost.exe) console windows.
Note:
For an ad hoc solution that uses different keys (strictly speaking, a keyboard shortcut followed by an additional, UI-language-dependent keypress), but requires no setup, see No Refunds No Returns's answer.
This answer provides a workaround that works transparently, i.e., with the original shortcut key, but it requires modification of your $PROFILE file; on the plus side, you can use the same technique to make other unavailable shortcuts available too.
It is the imported-by-default PSReadline module that prevents Alt+F4 from being effective[1].
While unloading PSReadline (Remove-Module -Force PSReadline) is an option in principle, you'll lose valuable command-line editing features if you do.
The best approach is therefore to define a Alt+F4 handler for PSReadline that emulates the default behavior:
Add the following to your $PROFILE, which sets up a PSReadline key-chord handler that uses the .SendKeys() method of the WScript.Shell COM object to send Alt+F4 to the caller's console window, which closes it:
Set-PSReadlineKeyHandler Alt+F4 -ScriptBlock {
(New-Object -ComObject WScript.Shell).SendKeys('%{F4}')
}
[1] As of v2.0.0-beta3. The longstanding underlying technical problem is explained in this GitHub issue, which also contains workarounds for other standard keyboard shortcuts, such as Ctrl+F.
Related
So when I am in in VSCode using PowerShell extension the parameter options do not show up. For example if I type in "new-azresourcegroup -name test -location " the optional locations do not populate so I have to manually type them in. The odd thing is if I go to the VSCode PowerShell Console, this feature works as intended and I can tab though the "-location" options and choose which one I want. While doing this I am connected to Azure. The weird thing is that the console works, but the window where you write your code does not which to me is odd as its my understanding that the window uses the console in the background (if that makes sense). Not sure if this is a PowerShell extension issue or a VSCode issue. Any help much appreciated.
The answer = CTRL + SPACEBAR will reveal the options. Would be nice if if could be switched to the TAB key so that it would match the behavior of commandline or terminal.
Motivation: I'm using a software that doesn't have an API to interface with ... I have no alternative and have to open the software, send simple key sequence, then close ... Again and again, so I want to automate this process.
Goal: Send combinations of keyboard inputs to an inactive window.
Progress: I wrote a powershell script that open, send keys, wait, then end the process, but it only works on active window. A part of the powershell code is as follows.
$appProcess = Start-Process -FilePath $path -PassThru
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($appProcess.Id)
$wshell.SendKeys('%(E)E')
Stop-Process $appProcess -Force
It works, but only on active window (windows comes to the top). What I want is to run automate the window in the background. I found an article that point me to using PostMessage in Win32 API. Since the majority of my code uses python, I decided to move from powershell to pywin32.
Issue: I cannot get the PostMessage to send key to the right handler. I saw in this article that I may need to find the exact window, but I still don't really understand how. In powershell, I can directly send keys via $wshell.AppActivate($appProcess.Id).
hwndMain = win32gui.FindWindow(None, winname)
hwndChild = win32gui.GetWindow(hwndMain, win32con.GW_CHILD)
temp = win32api.PostMessage(hwndChild, win32con.WM_KEYDOWN, 0x45, 0)
# temp came out as None
Question: Is there a way to do this in pywin32 / Win32 API?
Edit: (May 8, 2020) Yes, I have heard that using SendKeys are not reliable, but since there is no alternative offered to questions like this one on SO, how should anyone learn the "right way"? If you think there is alternative, everyone will appreciate to see a solution in action. Please suggest edits to my post to improve the quality of the question instead of shooting it down.
Time ago I developed SendMessage utility that allows to post messages to other processes or Windows via the WIN32 API. I discovered that I can't send a message to an inactive window, so I searched for a method to activate a Window. After several tests, I found a trick that allows me to "reactivate" a Window:
The development of this program presented a problem: the items in the
Notepad topmost menu can not be selected if the window was not active.
I did some tests and discovered that this point depends on the
relation that the cmd.exe window have (has?) with the other window and
the particular program in the other window. For example, in the case
of Calculator Windows accessory, its menu items can be selected when
the window is not active, and even when the window is minimized! I
tried to develop a method to activate other window from the cmd.exe
one using just System-Defined messages. I did several tests using
diverse combinations of WM_ACTIVATE, WM_CANCELMODE, WM_ENABLE,
WM_ACTIVATEAPP, WM_SETFOCUS and WM_KILLFOCUS messages, with no
success. Fortunately, the items in the system menu of any window can
be selected from another window, and after a SC_RESTORE the restored
window remains active; this behavior makes possible to activate
Notepad and other windows from the cmd.exe one via a Minimize/Restore
procedure.
I think that using my SendMessage.exe utility you may test if you may send keys to your inactive window process or to activate it, so you may then translate such a method to your phyton code. For complete details on this matter, see this link.
Powershell ISE terminal has the IntelliSense feature. Is it possible to get/enable the same IntelliSense feature in VSCode powershell terminal
Many thanks
The PowerShell extension(PSReadLine) for VSCode is still in pipeline for the next Enhancement, the teams are working on it should be released soon enough.
From what I read seems like a lot of work before they can get PSReadline to coexist with the PowerShell extension's language.
As per the comment from David Wilson from the discussion
"PSReadline is not supported in the integrated console at this time. A non-trivial amount of work is needed to make PowerShell Editor Services and PSReadline play nicely together so this could take some time.
Features this will enable:
1.Ctrl+Arrow keys, etc (keyboard navigation of the input line)
2.Syntax highlighting of input text
3.Multi-line text input (writing a function definition in the console across multiple lines)"
You can follow the discussion on
https://github.com/PowerShell/vscode-powershell/issues/535
The default PowerShell is not convinient to copy/paste command output. SublimeREPL solves this problem very well; however, there's no tab-completion. This made PowerShell hard to use. Is there a way to get the tab-completion?
Keyboard shortcuts can differ, based on your configuration and OS.
Have you tried Alt+/?
I'm running Console2, which I believe is just an interface to windows cmd
I have a custom batch file that does most of my dirty work for me, but there are lot of commands in it now. Is there a way I can get a tap autocomplete working for it in the windows command prompt?
For example: my script is called rob.bat and it takes in a various number of arguments
It'd like to type rob set{Tab} and then have it cycle through
setup_envvars
setup_userprefs
setup_whateverothersetupscriptsIhave
Is there a way to do this?
Console2 has no special provisions for tab completion and instead relies on the program running within it to provide such features. Picture Console2 as little more than something that runs a console program hidden somewhere, regularly polls that hidden window for changes, and forwards all input to that window; this is, in essence, what's happening.
Note that Console2 does nothing special with cmd. You can run any shell within it. As for customizing tab completion, cmd offers nothing of that sort. You may be able to change this by installing clink, which has extension points for Lua code. Another option would be PowerShell, which has customizable tab completion out of the box, either by wrapping your program in a function that provides the necessary parameters, or by writing a custom TabExpansion or TabExpansion2 function.