[Powershell] Fenster schließen anhand des Fenstertitels

Gepostet am: May 02, 2014 7:51:36 PM

Ziel ist es ein Fenster zu schließen anhand des Fenstertitels. Man muss dabei zwischen Programmen und Explorer-Fenster unterscheiden.

CloseWindowByTitle

Function CloseWindowByTitle([string]$TitleContains, [bool]$isExplorer, [bool]$isProgram) {     if ($isExplorer -eq $null) {         $isExplorer = $true     }     if ($isProgram -eq $null) {         $isExplorer = $true     }              if ($isProgram -eq $true)      {         Write-Host "Check for running Programs '$TitleContains'"             #All Processes        Get-Process | ForEach-Object {             $WindowName = $_.MainWindowTitle             if ((($WindowName).Trim()).length -ne 0) {                 if ($WindowName.Contains($TitleContains)) {                     Write-Host "'$WindowName' wird geschlossen"                     Stop-Process $_.ID                 }             }         }     }          if ($isExplorer -eq $true)      {         Write-Host "Check for running Explorer '$TitleContains'"             #All Shell Applications        $Applications = (New-Object -comObject Shell.Application).Windows()          $Applications | ForEach-Object {             $WindowName = $_.LocationName             if ($WindowName.Contains($TitleContains)) {                 Write-Host "'$WindowName' wird geschlossen"                 $_.Quit()             }         }     } }  CloseWindowByTitle -TitleContains "Command Prompt" -isExplorer $true -isProgram $false