Run Where-Object against string with multiple values

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Daniel Tobler Daniel Tobler 2 months, 1 week ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #47699
    Profile photo of Daniel Tobler
    Daniel Tobler
    Participant

    Hi Guys,

    I'm quite new to PowerShell and need some advise/help from you. The goal is to delete all mapped printers that are listed in a file.
    It works with only one printer name in the text file, but not with multiple. I'm using Windows 7 with SP1

    Thanks in advance for your help.

    Best regards,
    Daniel

    • This topic was modified 2 months, 1 week ago by Profile photo of Daniel Tobler Daniel Tobler.
    #47720

    Your where-object statement is actually comparing anything to the list of printers. Also its inefficient to read the file of printer names for every object on the pipeline

    Run this to test that you're seeing the printers you expect
    $printers = Get-Content C:\MFPs.txt
    Get-WmiObject -Class Win32_Printer | where Name -in $printers

    There isn't a Delete method on the Win23_Printer class so try
    Get-WmiObject -Class Win32_Printer | where Name -in $printers | Remove-WmiObject

    #47750
    Profile photo of Daniel Tobler
    Daniel Tobler
    Participant

    Hello,

    Thanks for your fast reply. The command you suggest is working as excepted, thanks a lot. Good hint regarding getting content of file.

     Get-WmiObject -Class Win32_Printer | Where-Object -Property ShareName -In $Printers | Remove-WmiObject 

    What is better, WmiObject or CimInstance? Does it matter?

    Do you have an idea or advise how to handle printers that are set as "default printers"? When I remove such a printer trough the script, none of the remaining printers get assigned as "default printer".

    Thanks and best regards,
    Daniel

    #47766
    Profile photo of guru
    guru
    Participant

    You can refer this link
    Difference between WMI and CIM

    • This reply was modified 2 months, 1 week ago by Profile photo of guru guru.
    #47840
    Profile photo of Daniel Tobler
    Daniel Tobler
    Participant

    Hi guru

    Thanks for the link.

    @Richard Siddaway, thanks for the post.

    Regards,
    Daniel

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.