function Get-NetshExcludedPortRanges { <# .SYNOPSIS Get the existing port ranges and parse them into actionable items #> [CmdletBinding()] [OutputType([object[]])] param ( ) $ranges = @() $existingExcludedPortRanges = (netsh int ipv4 show excludedportrange protocol=tcp) foreach($existingRange in $existingExcludedPortRanges) { $cleanedRange = $existingRange.Replace('Protocol tcp Port Exclusion Ranges','').Replace('* - Administered port exclusions.','').Replace('Start Port','').Replace('End Port','').Replace('--','').Trim() if ([string]::IsNullOrWhiteSpace($cleanedRange)) { continue } $splits = $cleanedRange -split '\s+' if ($splits.Length -gt 1) { $rangeStart = [int]$splits[0] $rangeEnd = [int]$splits[1] $ranges += @{ Start = $rangeStart; End = $rangeEnd;} } } return $ranges }