How to Get Window Titles Using PowerShell?

Recently, one of my team members asked for a script to get Windows titles. In this tutorial, I will explain how to get the window titles of active applications using PowerShell.

Basic Commands to Get Window Titles using PowerShell

Let’s start with the basics. To get the window titles of currently running applications, you can use the Get-Process cmdlet combined with filtering and formatting.

Example 1: List All Window Titles

The following script lists all window titles of currently running applications:

Get-Process | Where-Object { $_.MainWindowTitle } | Select-Object Id, ProcessName, MainWindowTitle

This script uses the Get-Process cmdlet to retrieve all running processes. It then filters out those without a window title using Where-Object { $_.MainWindowTitle }, and finally, it selects and displays the process ID, name, and window title.

You can see it displays the exact output in the screenshot below.

Get Window Titles using PowerShell

Check out Get Windows Services Using PowerShell

Example 2: Display Titles of Specific Applications

If you want to retrieve the window titles of specific applications, such as Google Chrome or Microsoft Word, you can modify the script as follows:

Get-Process -Name chrome, winword | Where-Object { $_.MainWindowTitle } | Select-Object Id, ProcessName, MainWindowTitle

This script specifically targets processes named chrome and winword (the process names for Google Chrome and Microsoft Word, respectively).

Here is the exact output in the screenshot below:

How to Get Window Titles Using PowerShell?

Example 3: Logging Active Window Titles

Suppose you want to log the titles of active windows over time. You can create a script that runs in the background and writes the window titles to a log file.

$logFile = "C:\Users\Public\window_titles_log.txt"

while ($true) {
    $activeWindow = Get-Process | Where-Object { $_.MainWindowTitle } | Select-Object -First 1 -Property MainWindowTitle
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $logEntry = "$timestamp - $($activeWindow.MainWindowTitle)"
    Add-Content -Path $logFile -Value $logEntry
    Start-Sleep -Seconds 10
}

This script runs indefinitely (while ($true)), retrieves the title of the first active window, and appends it to a log file with a timestamp every 10 seconds.

Check out Get Windows Activation Status Using PowerShell

Example 4: Automating Tasks Based on Window Titles

You can also automate tasks based on the active window title. For instance, if you want to close a specific application when its window title matches a certain string, use the following script:

$targetTitle = "Untitled - Notepad"

while ($true) {
    $activeWindow = Get-Process | Where-Object { $_.MainWindowTitle -eq $targetTitle }
    if ($activeWindow) {
        Stop-Process -Id $activeWindow.Id
        Write-Output "Closed application with title: $targetTitle"
    }
    Start-Sleep -Seconds 5
}

This script checks every 5 seconds if there is an active window with the title “Untitled – Notepad”. If found, it closes the application.

Sometimes, you may need to handle scenarios where multiple windows of the same application are open. Here’s how you can tackle this:

Example 5: Listing All Windows of a Specific Application

To list all window titles of a specific application, such as multiple instances of Google Chrome, use:

Get-Process -Name chrome | Where-Object { $_.MainWindowTitle } | Select-Object Id, ProcessName, MainWindowTitle

This script retrieves all instances of Google Chrome and lists their window titles.

Check out How to List Drives in PowerShell?

Example 6: Focus on a Specific Window

If you want to bring a specific window to the foreground based on its title, you can use the AppActivate method from the System.Windows.Forms namespace.

Add-Type -AssemblyName System.Windows.Forms
$targetTitle = "Google - Chrome"

$window = Get-Process | Where-Object { $_.MainWindowTitle -like "*$targetTitle*" }

if ($window) {
    [System.Windows.Forms.SendKeys]::SendWait("%{TAB}")
    [System.Windows.Forms.SendKeys]::SendWait($window.MainWindowTitle)
}

This script brings the window with the specified title to the foreground.

Conclusion

In this tutorial, I explained how to get Windows Titles using PowerShell. I have also shown you how to get the titles of specific applications using PowerShell. Also, I hope all the examples are helpful.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.