8

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

TITLE Here Are The New Contents

Image

cascading-style
  • 227
  • 1
  • 3
  • 12

5 Answers5

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
mklement0
  • 1,899
  • 1
  • 17
  • 22
Wael Boutglay
  • 278
  • 1
  • 8
  • 3
    `echo [system.console]::title` simply outputs `[system.console]::title` for me –  Nov 30 '18 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](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing). – mklement0 Oct 06 '19 at 19:31
4

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

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

AtomicFireball
  • 349
  • 1
  • 7
  • 3
    How can I get the correct instance of cmd.exe if multiple are running? – cascading-style Dec 12 '16 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 " /fo list /v | find "Window Title:` – AtomicFireball Dec 12 '16 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 May 07 '23 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 May 07 '23 at 18:56
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%"
mklement0
  • 1,899
  • 1
  • 17
  • 22
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