Powershell command to export users and group memberships from an OU to a CVS

Hi
 I would like assistance with creating a powershell  command that will do the following
 
    Export users and group memberships from an OU to  CVS file
     
      Then remove all the users from all the groups except the domain users group
       I  would appreciate any assistance
      LVL 1
      Phil MapfumoInfrastructure EngineerAsked:
      Who is Participating?
      I wear a lot of hats...

      "The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

      Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
      Change the Name of OU. ($OUName)
      This is the CSV part. it is missing the Removal part, because to test that I'll have to create some dummy groups I don't want to mess my AD.
      $OUName="ManagedServices"
      $outputfile= ".\MembersByGroup.csv"
      #Get DN of the group
      $DN=Get-ADOrganizationalUnit -Filter * | where { $_.distinguishedName -match "Managed"} | select -ExpandProperty DistinguishedName
      
      #Get all groups in that DN
      $groups=Get-ADGroup -SearchBase $DN  -Filter *
      #userMembership variable
      $UsersMembership=@()
      #iterate each group to find out the members
      foreach($group in $groups){
      #iterate the members to and create the new object and add it to the UsersMembershipArray.
          $members = Get-ADGroupMember -Identity $group
          if($members){
              foreach($member in $members){
                  $nobj= new-object psobject -Property @{"Group"=$group.Name;"user"=$member.name;"SamAccountName"=$member.SamAccountNAme;"DN"=$member.distinguishedName}
                  $UsersMembership+=$nobj
              }
          }
      }  
      #export to csv.
      $UsersMembership | Export-Csv -NoTypeInformation $membersByGroup
      
      write-host "are you sure you want to remove all members except ""Domain User's"" group?"
      #Remove-ADGroupMember
      

      Open in new window

      0

      Experts Exchange Solution brought to you by

      Your issues matter to us.

      Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

      Start your 7-day free trial
      Phil MapfumoInfrastructure EngineerAuthor Commented:
      Thanks Jose  can you please tell me which part of the script removes the all the users groups and leaves on the domain users as l may want to run that part of the command separately
      0
      Phil MapfumoInfrastructure EngineerAuthor Commented:
      Hi  Jose
       I have tried to run the following command below to move the users from an OU to a CSV but l keep getting an error saying in the text file attached:

      $OUName="Users"
      $outputfile= ".\MembersByGroup.csv"
      #Get DN of the group
      $DN=Get-ADOrganizationalUnit -Filter * | where { $_.distinguishedName -match "Managed"} | select -ExpandProperty DistinguishedName

      #Get all groups in that DN
      $groups=Get-ADGroup -SearchBase $DN  -Filter *
      #userMembership variable
      $UsersMembership=@()
      #iterate each group to find out the members
      foreach($group in $groups){
      #iterate the members to and create the new object and add it to the UsersMembershipArray.
          $members = Get-ADGroupMember -Identity $group
          if($members){
              foreach($member in $members){
                  $nobj= new-object psobject -Property @{"Group"=$group.Name;"user"=$member.name;"SamAccountName"=$member.SamAccountNAme;"DN"=$member.distinguishedName}
                  $UsersMembership+=$nobj
              }
          }
      }  
      #export to csv.
      $UsersMembership | Export-Csv -NoTypeInformation $membersByGroup
      Errors.txt
      0
      Introducing the "443 Security Simplified" Podcast

      This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

      Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
      The reason for this is because the "users" is not an OU it's a container, so you statements are bad.
      My script will work for Organizational units, not for containers.

      you need to make sure that your "organizational unit" is actually an organization unit.
      or provide the Distinguished name directly in line 7
      0
      Phil MapfumoInfrastructure EngineerAuthor Commented:
      Okay cool thanks l  will try another OU
      0
      Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
      Hi Phil Mapfumo, in the script is not implemented the removed part. I would need to set up a test environment, as I explained earlier, and I don't want to mess with my Productive AD.

      The variable of the groups and memberships are this:
      $UsersMembership=@()

      And the remove command is remove-adgroupmember -identity $identity

      jose
      0
      Seth SimmonsSr. Systems AdministratorCommented:
      No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

      I have recommended this question be closed as follows:

      Accept: Jose Gabriel Ortega C (https:#a42447192)

      If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

      seth2740
      Experts-Exchange Cleanup Volunteer
      0
      It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
      Powershell

      From novice to tech pro — start learning today.

      Question has a verified solution.

      Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

      Have a better answer? Share it in a comment.