Get csv from script

Welcome Forums General PowerShell Q&A Get csv from script

Viewing 2 reply threads
  • Author
    Posts
    • #52440
      JP
      Participant
      Topics: 1
      Replies: 2
      Points: 0
      Rank: Member

      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
      Participant
      Topics: 6
      Replies: 53
      Points: 0
      Rank: Member

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

      • #52470
        JP
        Participant
        Topics: 1
        Replies: 2
        Points: 0
        Rank: Member

        Correct.

        Thanks.

    • #52472
      Participant
      Topics: 10
      Replies: 1293
      Points: 1,121
      Helping Hand
      Rank: Community Hero

      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
        JP
        Participant
        Topics: 1
        Replies: 2
        Points: 0
        Rank: Member

        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

Viewing 2 reply threads
  • The topic ‘Get csv from script’ is closed to new replies.