Query for specific group membership, export to csv

This topic contains 2 replies, has 2 voices, and was last updated by  Carlo Mancini 3 years, 9 months ago.

  • Author
    Posts
  • #13255

    Jesse
    Participant

    I currently have a one liner that uses get-aduser to query for users that have not logged in and then I do some filtering. I have managed to get the memberof attribute to display correctly in a csv file. I would like to be able to sort on specific group membership. I orginally thought about splitting group membership into separate columns but I thought this might result in inconsistent results. How would I go about creating some custom columns in my csv file and doing boolean evaluation of specific group membership? If any more experienced powershell users have some better ideas than this, I would appreciate any help. I have about five groups that I want to be able to sort with.

    $Date = (Get-date).AddMonths(-26).Date
    get-aduser -Filter {(memberOf -ne "DN of group name")} -Properties DisplayName,SamAccountName,GivenName,Surname,StreetAddress,State,LastLogonDate,Created,EmailAddress,memberof,lastlogontimestamp -SearchBase "targetOU" -SearchScope Subtree -Server FQDN of server| Select DisplayName,SamAccountName,GivenName,Surname,StreetAddress,State,LastLogonDate,Created,EmailAddress,@{name='MemberOf'; expression={ ($_.MemberOf | ForEach { ($_ -split ',')[0] -replace 'CN=',"}) -join ','}},@{N='LastLogonTimestamp'; E={[DateTime]::FromFileTime($_.LastLogonTimestamp)}} | where-object {$_.lastlogontimestamp -lt $Date} | export-csv file path -NoTypeInformation

  • #13349

    Jesse
    Participant

    I figured something out on this, no help needed.

  • #13369

    Carlo Mancini
    Participant

    Jesse,
    how did you manage to solve your issue?
    Thanks

You must be logged in to reply to this topic.