Answered by:
Where to place Where-object command

Question
-
Please forgive my lack of PowerShell prowess - I am having trouble filtering my results with the Where-object command.
Get-ADUser -Filter {company -ne 'ABC' -and extensionAttribute4 -eq 'DEF'} -SearchBase "OU=Users,OU=Marketing,DC=GHI,DC=com"
-Properties SamAccountName,distinguishedName,sn,givenName,LastLogonDate,whenCreated,PasswordLastSet,Enabled |
Select SamAccountName,distinguishedName,sn,givenName,LastLogonDate,whenCreated,PasswordLastSet,Enabled |
Export-Csv ".\Export.csv" –NoTypeInformation -encoding UTF8
The above works OK, but I cannot figure out how to only export results to CSV where whenCreated and PasswordLastSet both match. I am only interested in those results.
I have been (unsuccesully) experimenting with the below:
Where-Object whenCreated -eq PasswordLastSet
Please help!
Wednesday, May 30, 2018 9:58 AM
Answers
-
$splat = @{ Filter = {company -ne 'ABC' -and extensionAttribute4 -eq 'DEF'} SearchBase = 'OU=Users,OU=Marketing,DC=GHI,DC=com' Properties = 'SamAccountName','distinguishedName','sn','givenName','LastLogonDate','whenCreated','PasswordLastSet','Enabled' } Get-ADUser @splat | Where-Object{$_.WhenCreated -eq $_.PassWordLastSet} | Select-Object $splat.properties | Export-Csv .\Export.csv –NoTypeInformation -encoding UTF8
\_(ツ)_/
- Edited by jrv Wednesday, May 30, 2018 10:34 AM
- Marked as answer by zerocritical Wednesday, May 30, 2018 11:55 AM
Wednesday, May 30, 2018 10:32 AM
All replies
-
Get-ADUser -Filter {company -ne 'ABC' -and extensionAttribute4 -eq 'DEF'} -SearchBase "OU=Users,OU=Marketing,DC=GHI,DC=com" -Properties SamAccountName,distinguishedName,sn,givenName,LastLogonDate,whenCreated,PasswordLastSet,Enabled | Select SamAccountName,distinguishedName,sn,givenName,LastLogonDate,whenCreated,PasswordLastSet,Enabled | Where-Object {$_.WhenCreated -eq $_.PasswordLastSet} | Export-Csv ".\Export.csv" –NoTypeInformation -encoding UTF8
For syntax and examples, consult full MS documentation or the cmdlet's page on SS64.
$_ stands for $PSItem you may also want to read up on.
- Edited by Paweł Balczunas Wednesday, May 30, 2018 10:12 AM
- Proposed as answer by Paweł Balczunas Wednesday, May 30, 2018 10:20 AM
Wednesday, May 30, 2018 10:10 AM -
$splat = @{ Filter = {company -ne 'ABC' -and extensionAttribute4 -eq 'DEF'} SearchBase = 'OU=Users,OU=Marketing,DC=GHI,DC=com' Properties = 'SamAccountName','distinguishedName','sn','givenName','LastLogonDate','whenCreated','PasswordLastSet','Enabled' } Get-ADUser @splat | Where-Object{$_.WhenCreated -eq $_.PassWordLastSet} | Select-Object $splat.properties | Export-Csv .\Export.csv –NoTypeInformation -encoding UTF8
\_(ツ)_/
- Edited by jrv Wednesday, May 30, 2018 10:34 AM
- Marked as answer by zerocritical Wednesday, May 30, 2018 11:55 AM
Wednesday, May 30, 2018 10:32 AM -
Guys, thank you so much for your help. Although I'm sure both examples work, I went with jrv's suggestion as it much clearer to read compared with the original code.
Just one thing to add and after doing a bit more research, I needed to append .DateTime to the attributes:
Where-Object{$_.WhenCreated.DateTime -eq $_.PasswordLastSet.DateTime}
Otherwise the CSV output only contained  in cell A1.
Wednesday, May 30, 2018 11:55 AM