8

How can I get the value of the current window's title, set like this:

TITLE Here Are The New Contents

Image

5 Answers 5

4

There's nothing built in, but you can retrieve it from the tasklist command.

tasklist /fi "imagename eq cmd.exe" /fo list /v

4
  • 3
    How can I get the correct instance of cmd.exe if multiple are running? Commented Dec 12, 2016 at 20:02
  • 4
    You 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 as tasklist /fi "pid eq <PID>" /fo list /v | find "Window Title: Commented Dec 12, 2016 at 20:16
  • While the command in the answer in combination with the previous comment provide all the ingredients for a complete solution, putting it all together is far from trivial, unfortunately. The next comment summarizes all required steps.
    – mklement0
    Commented May 7, 2023 at 18:56
  • You need an initial wmic call to determine the current cmd.exe' instance's PID (process ID), by parsing the wmic call's output. A temporary file is needed, because direct use of for /f with the wmic call would run it in a child cmd.exe process, which wouldn't work. After parsing the temporary file with for /f to extract the PID into variable, that variable can then be used in a tasklist call to get information about the current cmd.exe instance, including its window title. tasklist's output too must be parsed with for /f to extract the window title in isolation.
    – mklement0
    Commented May 7, 2023 at 18:56
9

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 output it directly:

[system.console]::title
2
  • 3
    echo [system.console]::title simply outputs [system.console]::title for me
    – user199731
    Commented Nov 30, 2018 at 12:48
  • 4
    @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.
    – mklement0
    Commented Oct 6, 2019 at 19:31
4

Calling PowerShell from your batch file via its CLI, powershell.exe, is easiest:

:: Outputs the window title.
powershell -noprofile -c [Console]::Title | findstr .

Note:

  • [Console]::Title returns the current console window's title using the System.Console .NET class; PowerShell provides access to all .NET types.

  • The findstr . command is a dummy command, which is necessary to prevent the output from containing a - <command-line> suffix, owing to the fact that cmd.exe appends such a suffix to the window title while a command line is executing (<command-line> here represents the specific command line invoked).
    Appending a suffix does not happen if a pipeline (|) is used, so the addition of | findstr . exe - which simply passes (non-empty) output through - is enough to prevent a suffix from showing in the result.

Complete example that shows how to capture the title in a variable:

@echo off & setlocal

:: Assign a custom title.
title This ^& That

:: Retrieve the current title and store it var. %thisTitle%
for /f "delims=" %%t in (
  'powershell -noprofile -c [Console]::Title ^| findstr .'
) do set thisTitle=%%t

echo This window's title: "%thisTitle%"

The above yields:

This window's title: "This & That"

More cumbersome alternative via wmic and tasklist:

Note:

  • The ingredients for this solution are in AtomicFireball's answer and a comment on it, but how to put them all together may not be obvious. The code below does that.

  • As you can see, the solution is much more complex compared to the PowerShell solution; note that using powershell.exe with the -c (-Command) parameter is not subject to the infamous PowerShell execution policy, so there should be no concern about using PowerShell for this task.

Complete example (same output as above):

:: Assign a custom title.
title This ^& That

:: Find the PID (process ID) of this cmd.exe session.
:: Note: A *temporary file* is required to capture the command output,
::       for later parsing. A `for /f` command cannot be used DIRECTLY
::       because it would execute the command in a *child* cmd.exe process, 
::       which would report the wrong PID.
:: Get a path for a temporary file.
set TEMPFILE=~getpid_%DATE%%TIME%.txt
set TEMPFILE=%TEMPFILE:/=%
set TEMPFILE=%TEMPFILE::=%
set TEMPFILE=%TEMP%\%TEMPFILE: =%
WMIC process get Name,ParentProcessId | findstr "^WMIC\.exe" > "%TEMPFILE%"
for /f "tokens=2" %%i in (%TEMPFILE%) do set PID=%%i
del "%TEMPFILE%"

:: Now use the PID to look up process details, which includes the window title.
for /f "tokens=1,* delims=:" %%i in (
  'tasklist /fi "PID eq %PID%" /fo list /v ^| findstr "^Window Title:'
) do set thisTitle=%%j
:: Trim the leading space:
for /f "tokens=*" %%i in ("%thisTitle%") do set thisTitle=%%i

echo This window's title: "%thisTitle%"
0

Or for short as a batch function:

rem # Assign a custom title.
title This ^& That

rem # Retrieve the current title.

CALL :getWindowTitle windowTitle
ECHO windowTitle="%windowTitle%".
GOTO :EOF

:getWindowTitle titlevar
    SETLOCAL
    FOR /f "usebackq delims=" %%t IN (`powershell -noprofile -c "[system.console]::title"`) DO SET "thisTitle=%%t"
    ENDLOCAL&CALL SET "%~1=%thisTitle%"
GOTO :EOF
-1

powershell ( Get-WmiObject Win32_Process -Filter ProcessId=$PID ).ParentProcessId

3
  • 4
    And then what? Commented Apr 19, 2019 at 15:12
  • tasklist /fi "pid eq 13368" /fo list /v | find /i "Window Title:"
    – Shen Tony
    Commented Nov 1, 2019 at 1:32
  • The last command returns: Window Title: N/A on Windows 11. Seems that the PowerShell command get the wrong ProcessID Commented May 7, 2023 at 5:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .