5

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

TITLE Here Are The New Contents

Image

| |
3

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

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

| |
  • 1
    How can I get the correct instance of cmd.exe if multiple are running? – cascading-style Dec 12 '16 at 20:02
  • 2
    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: – AtomicFireball Dec 12 '16 at 20:16
8

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
| |
  • 2
    echo [system.console]::title simply outputs [system.console]::title for me – a_horse_with_no_name Nov 30 '18 at 12:48
  • 2
    @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 Oct 6 '19 at 19:31
1

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"
| |
0

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

| |
  • 2
    And then what? – Scott Apr 19 '19 at 15:12
  • tasklist /fi "pid eq 13368" /fo list /v | find /i "Window Title:" – Shen Tony Nov 1 '19 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.