-
Notifications
You must be signed in to change notification settings - Fork 8.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Identify WindowsTerminal process ID #5694
Comments
Thanks for the request! I'm going to need a use case for this one o_O |
(You can use |
That PowerShell command only works in PowerShell 7, not Windows PowerShell. I am building a simple PowerShell tool that gets the Windows Terminal process and its children. This is not difficult if there is only one instance of Windows Terminal running. But I don't want to make that assumption. |
I have a viable workaround for my immediate need. Still, since you are already populating environment variables with profile_id and session, I'm hoping it isn't that hard to add wt_pid. |
Hey Jeff -- for windows powershell, use
|
That's the workaround I'm using. |
Ok, but how does that depend on the assumption that there's only one WT running? I'm not following. |
This workaround using WMI also solves the multiple instances issue. I originally started with a simple But I still think it would be handy to have a variable of some sort to indicate the process id just as we do with $pid in PowerShell. |
So, I've been hemming and hawing about this issue. I don't want to introduce another environment variable exposing WT state for the following reasons:
I'm not sure the benefit here outweighs the cost, especially since there's a viable workaround 😄 |
In PS7, |
Checking the parent process doesn't work if WT is the Default Terminal Application, and you launch a console app from a graphical UI app.
The console window is actually hosted by
... and each additional PowerShell tab launches a new pair of So, for the first case, I really couldn't find a perfect solution to get the exact WindowsTerminal.exe app. function Get-ConsoleHostProcessId {
# Save Original ConsoleHost title
$oldTitle=$host.ui.RawUI.WindowTitle;
# Set unique console title.
$r=(New-Guid);
$host.ui.RawUI.WindowTitle = "$r";
#Find console window by title, then find console PID.
$result = (tasklist.exe /FO LIST /FI "WINDOWTITLE eq $r") | Select-String -Pattern "PID:*" -Raw
if ($null -ne $result) {
$consolePid = [int]($result.SubString(5).Trim());
} else {
$consolePid = 0;
}
# Restore original ConsoleHost title.
$host.ui.RawUI.WindowTitle=$oldTitle;
return [int]$consolePid;
}
function Test-IsWindowsTerminal {
$consolePid = Get-ConsoleHostProcessId;
if ($consolePid -gt 0) {
return (Get-Process -PID (Get-ConsoleHostProcessId)).ProcessName -eq "WindowsTerminal";
}
return $false;
} |
@gerardog I've been struggling whether to publish my crazy attempt ... 🤣 Using Process Explorer I observed that the WindowsTerminal process has a handle to the Shell process open. Relying on the assumption that this is always the case I tried to write a piece of code that enumerates all open handles searching for the right process handle. Edit: I improved it a little ...
Edit2:
Last Edit: Output of the script, DefTerm vs. Conhost: |
Test method using Sysinternals handle tool |
Also leave here |
Also leave here - get PID of current seassion |
some Explain --- |
Works in pwsh. error in powershell.
|
change |
By January 2025 we do it this way, the window doesn’t get focus so you can’t know its title. Pwsh version:
Cmd batch version
now that wmic is gone then you can use one window to work on different projects The lesson we learned is always the same, we study your behavior and then we play everything in the hand. |
Description of the new feature/enhancement
I'd like a way to identify the process ID associated with a Windows Terminal session. In PowerShell, I can see the session and profile ID variables. And it is easy enough to run Get-Process and find the Windows Terminal process. But what if there are 2 instances running? How can I tell which ID goes with which?
Proposed technical implementation details (optional)
PowerShell has a built-in variable
$pid
that helps me identify the current PowerShell process. It would be nice to either create a variable like$wtpid
. Or add another environment variable that identifies the process ID of this Windows Terminal instance.The text was updated successfully, but these errors were encountered: