Public/Remove-SpecPrinterPort.ps1

Function Remove-SpecPrinterPort {
    <#
    .SYNOPSIS
    This PowerShell function removes a specified printer port.
 
    .DESCRIPTION
    The Remove-SpecPrinterPort function removes a printer port by its name. It uses the Remove-PrinterPort cmdlet to delete the port. If the port is successfully removed, it returns a success code of 800. If the specified port does not exist, it returns an error code of 801. If an unexpected error occurs during the process, it returns an error code of 802.
 
    .PARAMETER portName
    Specifies the name of the printer port to remove. This parameter is mandatory.
 
    .EXAMPLE
    Remove-SpecPrinterPort -portName "COM1"
 
    This example removes the printer port named "COM1".
 
    .INPUTS
    None. You cannot pipe objects to this function.
 
    .OUTPUTS
    Returns an array of codes including if any errors occurred during the process. The following codes can be returned:
    800 - The printer port was successfully removed.
    801 - The specified printer port does not exist.
    802 - An unexpected error occurred during the process. (Check the error message for more information.)
 
    .NOTES
    Author: owen.heaume
    Version: 1.0
    #>


    [cmdletbinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$portName
    )

    try {
        Write-Verbose "removing printer port $portName"
        Remove-PrinterPort $portName -ErrorAction Stop -ErrorVariable x
        Write-Verbose "printer $printer port deleted"
        return 800
    } Catch {
        Write-Warning "failed to remove printer port $portName"
        if ($x -match "No MSFT_PrinterPort objects found with property 'Name' equal to") {
            Write-Warning "No MSFT_PrinterPort objects found with property 'Name' equal to $portName"
            return 801
        } else {
            write-warning "The error message was $x"
            return 802
        }
    }
}