How to get the window title text from batch file

15,937

Solution 1

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

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

Solution 2

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

Solution 3

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"
Share:
15,937

Related videos on Youtube

cascading-style
Author by

cascading-style

Um... Hi? I'm CSS... I make web sites look cool... Since of my coolness rubs off on them... Find me on chess.com if you want... Handle: gssleader ... So... That pretty much sums me up.

Updated on September 18, 2022

Comments

  • cascading-style
    cascading-style almost 2 years

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

    TITLE Here Are The New Contents
    

    Image

  • cascading-style
    cascading-style over 7 years
    How can I get the correct instance of cmd.exe if multiple are running?
  • AtomicFireball
    AtomicFireball over 7 years
    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:
  • a_horse_with_no_name
    a_horse_with_no_name over 5 years
    echo [system.console]::title simply outputs [system.console]::title for me
  • Scott - Слава Україні
    Scott - Слава Україні about 5 years
    And then what?
  • mklement0
    mklement0 over 4 years
    @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.
  • Shen Tony
    Shen Tony over 4 years
    tasklist /fi "pid eq 13368" /fo list /v | find /i "Window Title:"