The script that I put together does gather the results I need, but I was hoping to utilize a cleaner string.
After I gather all the needed information, I export the results to a csv file. There are three specific rows I would like to remove, so I re-import the csv file and delete those rows. This is the string I am currently utilizing:
#Remove Unneeded Users
import-csv .\LDAPCrossWalkTable.csv | Where-Object "User ID" -ne 'test' | Where-Object "User ID" -ne 'process' | Where-Object "User ID" -ne 'app' | export-csv .\LDAPCrossWalkTableFinal.csv -Force -NoTypeInformation
Is there a way to use the Where-Object cmdlet one time by using the -or operator rather than pipe 3 separate times? I just can't figure out the correct syntax. Any input it greatly appreciated! Thanks!
UPDATE: Thank you, Olaf, for the prompt response and multiple options! I decided to proceed with this string:
import-csv .\LDAPCrossWalkTable.csv | Where-Object {$_.'User ID' -notin 'test', 'process', 'app'} | export-csv .\LDAPCrossWalkTableFinal.csv -Force -NoTypeInformation
Yes, there is a way to combine more than one filter in one Where-Object
. And you can apply this filter before you export your results to a CSV file
Where-Object {
$_.'User ID' -ne 'test' -and
$_.'User ID' -ne 'process' -and
$_.'User ID' -ne 'app'
}
As pointed out by the comment of @Santiago Squarzon ... and alternative to having multiple -ne
would be to use -notin
.
Where-Object {
$_.'User ID' -notin 'test', 'process', 'app'
}
... or ... as suggested like this:
Where-Object 'User ID' -notin 'test', 'process', 'app'
A third way recommended by @Theo would be top use regex like this:
Where-Object 'User ID' -notmatch '\b(test|process|app)\b'
or like this if it should be a full match
Where-Object 'User ID' -notmatch '^(test|process|app)$'
Now you have plenty of options to choose from. ;-)