How to get the window title text from batch file
There's nothing built in, but you can retrieve it from the tasklist
command.
tasklist /fi "imagename eq cmd.exe" /fo list /v
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
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
)- For instance, your batch file may temporarily set a different title and therefore will want to restore the original title before exiting.
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"