Powershell Script to remove multiple printers remotely

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Iron Iron 2 months ago.

  • Author
    Posts
  • #62256
    Profile photo of jeffery corbin
    jeffery corbin
    Participant

    need to remove multiple printers labeled 37 off a remote pc using power shell.

    Get-Printer -ComputerName computer | where name -match "37*" | Remove-printer

    returns the error Get-printer : the spooler service is not reachable. Service is running.

    $a = get-wmiobject -computername computer -query "SELECT * FROM win_32printer WHERE name = '30'" $a.delete()

    returns the error you cannot call a method on a null-valued expression.

  • #62344
    Profile photo of Iron
    Iron
    Participant

    So a few things.

    First, make sure you are actually able to connect to WinRM via powershell remoting. Try a simple enter-pssession -ComputerName "Computer" to see if you have this connectivity. Once in the pssession, try the get-printer command and make sure you're getting that far.

    As far as I know, the get-printer cmdlet only works on Windows 10 machines, but I could be wrong (I work in a mostly W7 environment).

    Another thing to consider is that how the printers were installed are going to play a huge factor in whether you can manage them or not. If users are accessing a print server on the network and installing their queue's that way (or through user based gpo's), then you're pretty much out of luck. These printers are recorded in the local users hive and not easily removed without:
    a. Logging in as the user or using their credentials (not good)
    b. Loading the user's hive when they are not logged in and clearing the HKCU\Printers\Connections\"PrinterName" key.

    Finally, if the printers are locally installed under the system account and you are able to find them through get-wmi object, try the following code to invoke the WMIObject method.

     Invoke-Command -ComputerName computer -Scriptblock {get-wmiobject -Class win32_printer | where {$_.Name -eq "PrinterName"} | foreach {$_.Delete()}}
    

You must be logged in to reply to this topic.