0
votes

I am trying to get a powershell script together to go through all users in our domain and find if they have a SIDhistory located in the SIDhistory attribute or not. I then need to export this list of users to a csv as i will need to delete all the accounts without a SIDhistory. I'm still learning powershell but this is what I have so far to find individual user's sidhistory. ( export CSV portion isn't working correctly )

dsquery * -Filter "(samaccountname=USERID)" -Attr samAccountName ObjectSID sidHistory | export-csv -path C:\Desktop\insertcsv.csv -notypeinformation

Thank you any help you can provide, I really appreciate it.

tl;dr - I need to pull all users with an empty SIDhistory attribute to a csv

1
How about something like get-aduser -filter {!(SIDhistory = *)} -prop ObjectSID,sidHistory|Select SamAccountName,ObjectSID,sidHistory|ExportCsv $env:USERPROFILE\Desktop\NoSIDHistory.csv -notypeTheMadTechnician

1 Answers

1
votes

This is pretty easy!

Get-aduser -filter * -properties sidhistory | Where sidhistory

This will first return all users, then instruct PowerShell to also return the sidhistory property if it exists. Then we filter using Where-Object to only return the accounts which have that property.