Ziel ist es ein Fenster zu schließen anhand des Fenstertitels. Man muss dabei zwischen Programmen und Explorer-Fenster unterscheiden.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |