foreach that finds if Service Account is a memberOf

Welcome Forums General PowerShell Q&A foreach that finds if Service Account is a memberOf

This topic contains 2 replies, has 2 voices, and was last updated by

6 months, 3 weeks ago.

  • Author
  • #105671

    Points: 71
    Rank: Member

    I have started a script that finds all user accounts that are service accounts (their password never expire):

    $CorpSvcAccts = Get-ADUser -filter * -Properties PasswordNeverExpires |
        select name,PasswordNeverExpires,MemberOf | Where-Object {$_.PasswordNeverExpires -like "True"}
     foreach ($SvcAccts in $CorpSvcAccts)   {
        select memberOf -ExpandProperty memberOf $_. ??

    ...but wish to THEN discover if they are part of a Group called ServiceAccts. I want to export those results. Maybe one column that shows the User account that are TRUE (have membership) and another column that shows FALSE.

    I basically want to see each of the user accounts that have pwd set to never expire and if they're in the Security Group or not, exported.

    Thank you

  • #105682

    Points: 741
    Helping Hand
    Rank: Major Contributor

    You like to make it harder than needed, don't you? 😉

    $DesiredGroup = Get-ADGroup -Identity 'Name of the Group' | Select-Object -ExpandProperty DistinguishedName
    Get-ADUser -Filter "PasswordNeverExpires -eq '$true'" -Properties PasswordNeverExpires,MemberOf |
        Select-Object -Property Name,PasswordNeverExpires,
            Expression={If($_.MemberOf -contains $DesiredGroup){$True}Else{$false}}
    • #105700

      Points: 71
      Rank: Member

      Well, I certainly don't intend to but it may appear that way to you. It's a slow learning curve for me.

The topic ‘foreach that finds if Service Account is a memberOf’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort