10

I'm building a script that lists all Inactive computer accounts. I'd like to exclude a few systems from the results.

I've got a text-file containing all systems to be excluded (one systemname per line). All items are stored in an object with property name "name". So $excluded will contain:

name
----
system1
system2

To list all inactive systems I use the Search-ADAccount cmdlet:

$InactiveComputers = Search-ADAccount -AccountInactive -TimeSpan 90 -ComputersOnly | Where {$_.Enabled -eq $true}

Of course I can loop all results 1 by 1, but is there a simple way to exclude the systems directly from the results? I've got a feeling it's possible with select-object or where-object, but I can't figure out how to compare against the results in an object.

3 Answers 3

24

You were basically correct in using this in your title: "where {_.Name not in $object}"

Syntax is a little different. Pipe it to the following

Where { !($_.Name -in $excluded) }

OR

Where { $_.Name -notin $excluded }

Both seem to give the same results in the console. Happy coding!

Note: Tested this on PSv2 and v3.

I ran across this when looking for an answer and figured I would update with these options for others that run into this.

Sign up to request clarification or add additional context in comments.

2 Comments

What was the $PSVersionTable.PSVersion value of the PSv2 you tested?
@Matthew Sorry, I was running this on a work laptop which I don't have anymore so I can't say for sure. I don't recall what the exact powershell minor version was. Only recall the major versions. I did some digging and found this: technet.microsoft.com/en-us/library/hh847759.aspx It shows that Powershell v3 introduces -notin so you would have to use the first method to get this one to work in v2 ( Where { !($_.Name -in $excluded) } ). Hope this helps!
4

Import the exclude file (as csv) and use the -notcontains operator:

$names = Import-csv exclude.txt | Foreach-Object {$_.Name} 

$InactiveComputers = Search-ADAccount -AccountInactive -TimeSpan 90 -ComputersOnly | Where {$_.Enabled -eq $true -and $names -notcontains $_.Name}

6 Comments

That's not possible: the exclusion file actually also contains comments (= a line starting with a ;) and blanc lines. I don't want them included in my $exclused object.
You didn't mention this in your question. You could workaround it with: Import-csv exclude.txt | where {$_.Name -match '\S' -and $_.Name -notlike ";*"}
thanks for the tips. Unfortunately this command returns an empty object. import-csv excluded.txt works. But when I add the foreach or where-statement it returns an empty set
ignore that last comment. My bad.import-csv combined with where works.
but infortunately this didn't work. Search-ADaccounts stil includes the excluded items too :(
|
1

I think you can use -notcontains (TechNet article) operator:

$InactiveComputers = Search-ADAccount -AccountInactive -TimeSpan 90 -ComputersOnly | Where {$_.Enabled -eq $true -and $excluded -notcontains $_.name }

1 Comment

sorry, I tried that before, but it's not working. I think this is because $InActiveComputers contains more items (SamAccountName, Name, Enabled, SID, ...) while $Excluded only contains "name". How does where-object know how to match them?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.