function Add-ExcludedPortRange { param ( [Parameter(Mandatory = $true)][int]$startport, [Parameter(Mandatory = $true)][int]$numberofports ) Invoke-Expression "net stop winnat" foreach ($ip in ("ipv4", "ipv6")) { foreach ($protocol in ("tcp", "udp")) { $showCommand = "netsh interface $ip show excludedportrange protocol=$protocol" $addCommand = "netsh interface $ip add excludedportrange protocol=$protocol startport=$startport numberofports=$numberofports" Write-Host "$ $showCommand" Invoke-Expression $showCommand Write-Host "$ $addCommand" Invoke-Expression $addCommand } } Invoke-Expression "net start winnat" }