locked
Can where-object be used this way? RRS feed

  • Question

  • New to PS and I've had some success leveraging the following command to accomplish whats needed.  Fast forward to now the method I have been using is no longer practical because the list of -and is growing making it more difficult to manage.  Reading up on a few things I think leveraging an exclusion list txt file I build and call might be a good solution, but I'm having trouble getting it to work.  Any advice on how I can accomplish this would be greatly appreciated.

    Current script works great (although this is only a portion of the overall script hints the issue):

    Get-Content C:\computers.txt | Where-Object {($_ -notlike "*TREE*") -and ($_ -notlike "*MEAS*") -and ($_ -notlike "*LEVR*") -and ($_ -notlike "*RED*") -and ($_ -notmatch "serverb.domain.net")} | Set-Content C:\out.csv

    The idea is to get-content on txt file and use a variable to identify a server list that I want to exclude then piping out to a set-content for the legit list I need.  In the example below I'm having trouble getting the $exclude list removed from the computers.txt file.  Tried quite a few things an no luck thus far.

    $exclude = Get-Content C:\exclude.txt
    Get-Content C:\computers.txt | Where-Object {($_ -notlike $exclude)} | Set-Content C:\out.csv

    Thanks,

    Blind


    • Edited by Blindf8th Wednesday, November 29, 2017 3:36 PM
    Wednesday, November 29, 2017 3:35 PM

All replies

  • $exclude is an array, so I think but not sure you'll want to do it this way

    $exclude = Get-Content C:\exclude.txt
    Get-Content C:\computers.txt | Where-Object {($exclude -notcontains $_)} | Set-Content C:\out.csv


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Wednesday, November 29, 2017 3:42 PM
  • Or:

    $exclude = (Get-Content C:\exclude.txt) -join '|'
    Get-Content C:\computers.txt | Where-Object {$_ -notmatch $exclude} | 
    Set-Content C:\out.csv

    • Edited by JS2010 Wednesday, November 29, 2017 3:49 PM
    Wednesday, November 29, 2017 3:49 PM
  • The best\worse thing about this is that you did that in no time and I've been struggling with the syntax lol.  I actually was using -nomatch as well, but did not have the $_ at the end.  Guess I need to read up on what the $_ and $_. actually does, but in this case I should have started with the variable $exclude.  I would have never figured out the second method using the -join

    Cant thank you enough sir.  Much appreciated!

    Thank you,

    Blind

     
    • Edited by Blindf8th Wednesday, November 29, 2017 4:15 PM
    Wednesday, November 29, 2017 4:09 PM
  • In my case I'm also joining the whole list into one line with '|' in-between each one. I'm not sure what the limit of that is.

    • Edited by JS2010 Wednesday, November 29, 2017 4:18 PM
    Wednesday, November 29, 2017 4:13 PM
  • Use an array.  An array is virtually unlimited (2Gb )

    $exclude = Get-ContentC:\exclude.txt
    Get-ContentC:\computers.txt | Where-Object {$_ -notin $exclude}


    \_(ツ)_/

    Wednesday, November 29, 2017 5:14 PM
  • A problem with -match is that substrings could be true.

    Wednesday, November 29, 2017 7:02 PM
  • A problem with -match is that substrings could be true.

    Its RegEx so substrings can be eliminated by including word breaks. 

    '\bone\b|\btwo\b|three'


    \_(ツ)_/

    Wednesday, November 29, 2017 7:05 PM
  • So maybe this is better:

    $exclude = (Get-Content C:\exclude.txt) -join '\b|'

    Takes 20 seconds with a million lines.





    Wednesday, November 29, 2017 7:44 PM