Powershell: where {_.Name not in $object} Powershell: where {_.Name not in $object} powershell powershell

Powershell: where {_.Name not in $object}


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.


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}


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

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