Public/Remove-SpecPrinter.ps1

function Remove-SpecPrinter {
    <#
    .SYNOPSIS
    Removes a specified printer.
 
    .DESCRIPTION
    The Remove-SpecPrinter function deletes a printer from the system. It attempts to remove the specified printer and returns error codes indicating the success or failure of the operation.
 
    .PARAMETER printerName
    Specifies the name of the printer to be removed. This parameter is mandatory.
 
    .OUTPUTS
    Returns a code indicating the status of the operation. The following codes can be returned:
 
    600: Printer successfully deleted.
    601: Printer name not found. Failed to delete printer.
    602: Unknown error occurred. (See error message for details.)
 
    .EXAMPLE
    Remove-SpecPrinter -printerName "Printer1"
    Removes the printer named "Printer1". If successful, returns an error code of 600.
 
    .NOTES
    Author: owen.heaume
    Version: 1.0
    #>


    [cmdletbinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        $printerName
    )

    begin { }

    process {
        # delete the printer
        Write-Verbose "Deleting printer $printerName from $printerPort"
        try {
            Remove-Printer $printerName -ErrorAction stop -ErrorVariable x
            Write-Verbose "Printer $printerName deleted"
            return 600
        } Catch {
            Write-Warning "failed to remove printer $printerName"
            if ($x -match "No MSFT_Printer objects found with property 'Name' equal to") {
                Write-Warning "No MSFT_Printer objects found with property 'Name' equal to $printerName"
                return 601
            } else {
                write-warning "The error message was $x"
                return 602
            }
        }
    }
}