Get csv from script

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of JP JP 3 months ago.

  • Author
    Posts
  • #52440
    Profile photo of JP
    JP
    Participant

    Hi all,

    I have script which shows me all disabled users in security groups in OU. Now what I should get it is that I get .csv file with results.

    Script:
    $searchOU = "OU=,OU=,OU=,DC=test,DC="

    Get-ADGroup -Filter 'GroupCategory -eq "Security"' -SearchBase $searchOU | ForEach-Object{ $group = $_
    Get-ADGroupMember -Identity $group -Recursive | %{Get-ADUser -Identity $_.distinguishedName -Properties Enabled | ?{$_.Enabled -eq $false}} | ForEach-Object{ $user = $_
    $uname = $user.Name
    $gname = $group.Name
    Write-Output "DisabledUser $uname is a Member of $gname Security Group" -Foreground Yellow
    }
    }

    Thanks

  • #52464
    Profile photo of Paul Frankovich
    Paul Frankovich
    Participant

    You are trying to get a CSV that shows the group and which users are disabled?

    • #52470
      Profile photo of JP
      JP
      Participant

      Correct.

      Thanks.

  • #52472
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Reformatted so your code is a bit more readeable. Not tested, but should be close:

    $searchOU = "OU=,OU=,OU=,DC=test,DC="
    
    $results = foreach ( $group in ( Get-ADGroup -Filter 'GroupCategory -eq "Security"' -SearchBase $searchOU ) ) {
        foreach ( $groupMember in ( Get-ADGroupMember -Identity $group -Recursive ) ) {
            $user = Get-ADUser -Identity $groupMember.distinguishedName -Filter "Enabled -eq 'false'"
            if ( $user ) {
                $user | Select Name, @{Name="Group";Expression={$group.Name}}
            }
        }
    } 
    
    $results | Export-CSV -Path C:\DisabledUsersInGroups.csv -NoTypeInformation
    
    • #52507
      Profile photo of JP
      JP
      Participant

      Thanks Rob!

      It was very helpful.

      The script which is working is below:

      $searchOU = "OU=enter your OU,DC=domain,DC=domain"

      $results = foreach ( $group in ( Get-ADGroup -Filter 'GroupCategory -eq "Security"' -SearchBase $searchOU ) ) {
      foreach ( $groupMember in ( Get-ADGroupMember -Identity $group -Recursive ) ) {
      $user = Get-ADUser -Identity $groupMember.distinguishedName -Properties Enabled | ?{$_.Enabled -eq $false}
      if ( $user ) {
      $user | Select Name, @{Name="Group";Expression={$group.Name}}
      }
      }
      }

      $results | Export-CSV -Path C:\DisabledUsersInGroups.csv -NoTypeInformation

You must be logged in to reply to this topic.