9

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

TITLE Here Are The New Contents

Image

cascading-style
  • 247
  • 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
  • 2,148
5

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

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

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
  • 2,148
1

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 &quot;[system.console]::title&quot;) DO SET "thisTitle=%%t" ENDLOCAL&CALL SET "%~1=%thisTitle%" GOTO :EOF

Erik Bachmann
  • 116
  • 1
  • 4
-1

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