2
There's nothing built in, but you can retrieve it from the tasklist
command.
tasklist /fi "imagename eq cmd.exe" /fo list /v
5
In cmd.exe (usual command line prompt):
Set window's title:
title "Your New Title"
Get window's title: I didn't found anything useful to do such thing, However if you have some knowledge with C# or Visual Basic, you can develop a little program that will look in opened windows to find your command line and return the title for you. (using the PID of parent process (your cmd.exe))
In Powershell: (things are easy here)
Set window's title:
[system.console]::title = "Your New Title"
Get window's title:
$myTitleVar = [system.console]::title
or you can directly:
echo [system.console]::title
echo [system.console]::title
simply outputs [system.console]::title
for me – a_horse_with_no_name – 2018-11-30T12:48:50.200
@a_horse_with_no_name: No need to use echo
- just submit [system.console]::title
as-is (PowerShell implicitly outputs [to the display]). If you do use echo
(which is an alias for Write-Output
, whose explicit use is rarely needed), you must enclose the argument in (...)
: echo ([system.console]::title)
- in command arguments, a token-initial [
isn't evaluated as an expression and considered a string literal; see about_Parsing.
1
From a batch file, calling PowerShell is easiest (though it won't be fast):
powershell -noprofile -c "[Console]::Title.Replace(' - '+[Environment]::CommandLine,'')"
The above takes advantage of the fact that cmd.exe
appends - <command-line>
to the window title while executing an external program.
Note:
If your batch file is directly invoked and the window title was set beforehand, the title will include your batch file's own invocation as a suffix (e.g, Command Prompt - some.cmd
)
However, if your batch file is called from another batch file, and it is the latter that sets the title, your batch file's invocation will not that title.
In the former case, use the following variant if you want to remove the own-invocation suffix from the title:
powershell -noprofile -c "[Console]::Title.Replace(' - '+[Environment]::CommandLine,'') -replace '(.+) - .+'"
Complete example:
@echo off
setlocal
rem # Assign a custom title.
title This ^& That
rem # Retrieve the current title.
for /f "usebackq delims=" %%t in (`powershell -noprofile -c "[Console]::Title.Replace(' - '+[Environment]::CommandLine,'')"`) do set thisTitle=%%t
echo This window's title: "%thisTitle%"
The above yields:
This window's title: "This & That"
0
powershell ( Get-WmiObject Win32_Process -Filter ProcessId=$PID ).ParentProcessId
1And then what? – Scott – 2019-04-19T15:12:24.670
tasklist /fi "pid eq 13368" /fo list /v | find /i "Window Title:" – Shen Tony – 2019-11-01T01:32:28.750
How can I get the correct instance of cmd.exe if multiple are running? – cascading-style – 2016-12-12T20:02:32.640
2You can run
wmic process get parentprocessid,name|find "WMIC"
which returns the parent PID of the executing instance of cmd.exe. You can then parse the string (perhaps a for loop) to extract the PID and run against tasklist astasklist /fi "pid eq <PID>" /fo list /v | find "Window Title:
– AtomicFireball – 2016-12-12T20:16:22.347