none
PowerShell remove printers RRS feed

  • Question

  • Hi everybody,

    I need to remove/delete several printers from our print server. Thought it would be cool to use PowerShell instead of doing the job manually. Anyway, I created a csv file with the printer names and port names. When I run the script I get the following message...

    You cannot call a method on a null-valued expression.
    At C:\PS-scripts\BulkPrinterRemoval-Test.ps1:3 char:14
    +  $port.Delete <<<< ()
        + CategoryInfo          : InvalidOperation: (Delete:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    +  $port.Delete <<<< ()
        + CategoryInfo          : InvalidOperation: (Delete:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    Exception calling "Delete" with "0" argument(s): "Generic failure "
    At C:\PS-scripts\BulkPrinterRemoval-Test.ps1:9 char:14
    +  $port.Delete <<<< ()
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    Here is the script...

     function DeletePrinter {
     $port=gwmi Win32_Printer -filter "Name='$($printer.printername)'" -EnableAllPrivileges
     $port.Delete()
    }
    function DeletePrinterPort {
    $port=gwmi Win32_TCPIPPrinterPort -filter "Name='$($printer.portname)'" -EnableAllPrivileges
     $port.Delete()
    }
    $printers = Import-Csv "C:\printers.csv"
    foreach ($printer in $printers) {
     DeletePrinter $printer.printername
     DeletePrinterPort $printer.Portname
     }




    • Edited by Flite23 Wednesday, May 28, 2014 7:17 PM
    Wednesday, May 28, 2014 7:07 PM

Answers

  • You need to study how to call a function, How to pass a parameter and how to trap an error.

    Sample:

    function DeletePrinter{
         Param(
              $printer
         )
         Try{
              if($printer=gwmi Win32_Printer -filter "Name='$printer'"){
              	$printer.Delete()
    }else{
    Write-Host 'Printer not found' -fore red
    } } Catch{ Write-Host "$_" } }

    The rest of your functions are up to you.  If I were you I would avoid functions unti9l you are proficient at basic PowerShell.


    ¯\_(ツ)_/¯



    • Edited by jrv Wednesday, May 28, 2014 7:43 PM
    • Marked as answer by Flite23 Wednesday, June 4, 2014 5:19 PM
    Wednesday, May 28, 2014 7:35 PM

All replies

  • You need to study how to call a function, How to pass a parameter and how to trap an error.

    Sample:

    function DeletePrinter{
         Param(
              $printer
         )
         Try{
              if($printer=gwmi Win32_Printer -filter "Name='$printer'"){
              	$printer.Delete()
    }else{
    Write-Host 'Printer not found' -fore red
    } } Catch{ Write-Host "$_" } }

    The rest of your functions are up to you.  If I were you I would avoid functions unti9l you are proficient at basic PowerShell.


    ¯\_(ツ)_/¯



    • Edited by jrv Wednesday, May 28, 2014 7:43 PM
    • Marked as answer by Flite23 Wednesday, June 4, 2014 5:19 PM
    Wednesday, May 28, 2014 7:35 PM
  • Other things to consider:

    You cannot delete a port that is in use.

    You cannot delete a printer object as Win32_Printer has no "delete" method.

    An network attached printer has a delete.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, May 28, 2014 7:42 PM
    Wednesday, May 28, 2014 7:35 PM
  • Thanks for the help and advice. The script is working now.



    • Edited by Flite23 Wednesday, June 4, 2014 5:21 PM
    Wednesday, June 4, 2014 5:20 PM