function Find-MergedGitBranches { [CmdletBinding()] param () $alwaysKeep = @('main','master','develop','release') $mergedLines = Invoke-GitCommand -Verb branch -Arguments @("--merged") $branches = @() foreach ($line in $mergedLines) { $trimLine = $line.Trim() if ($trimLine.StartsWith("*") -or $alwaysKeep -contains $trimLine) { continue } else { $branches += $trimLine } } # Look for branches removed on origin $goneLines = Invoke-GitCommand -Verb branch -Arguments @("-vv") foreach ($line in $goneLines) { if ($line -match 'gone') { # magic string split $line = ($line -split '\[')[0] # Remove the committish at the end of the line $line = $line.Substring(0, $_.Length - 9) $trimLine = $line.Trim() if ($trimLine.StartsWith("*") -or $alwaysKeep -contains $trimLine) { continue } else { $branches += $trimLine } } } return $branches }