public/interface/portproxy/Remove-PortProxy.ps1

#Requires -RunAsAdministrator
function Remove-PortProxy {
    <#
    .SYNOPSIS
        Gets all configured v4tov4 portproxy entries and returns them as pscustomobjects
    .DESCRIPTION
        Gets all configured v4tov4 portproxy entries and returns them as pscustomobjects. This
        cmdlet only works with v4tov4 portproxy entries.
    .EXAMPLE
        PS C:\> Get-PortProxy -ListenPort 8000 | Remove-PortProxy
        Retrieves all portproxy entries where ListenPort is 8000 and removes them.
    .EXAMPLE
        PS C:\> Remove-PortProxy -ListenPort 8000 -ListenAddress 127.0.0.1
        Removes the portproxy entry where ListenPort is 8000 and the ListenAddress is the localhost
        interface 127.0.0.1. If this portproxy entry doesn't exist, a non-terminating error will be
        written.
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
        param(
            # Specifies the listen port for which all portproxy entries should be removed
            [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
            [ValidateRange(0,65535)]
            [int]
            $ListenPort,

            # Specifies the listen address for which all portproxy entries should be removed
            [Parameter(ValueFromPipelineByPropertyName)]
            [string]
            $ListenAddress
        )
        
        process {
            $command = "netsh.exe interface portproxy delete v4tov4 listenport=$ListenPort"
            if ($PSBoundParameters.ContainsKey('ListenAddress')) {
                $command += " listenaddress=$ListenAddress"
            }
            Write-Verbose "Executing the command '$command'"
            if ($PSCmdlet.ShouldProcess((hostname), $command)) {
                $output = Invoke-Expression -Command $command
                $success = $LASTEXITCODE -eq 0
                $output = [string]::Join("`r`n", $output).Trim()
                if ($success) {
                    Write-Information $output
                }
                else {
                    Write-Error "Error: $output"
                }
            }
        }
    }