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?

      [Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

      x
      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 CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
      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

      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
      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
      Determine the Perfect Price for Your IT Services

      Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

      Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
      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
      Phil MapfumoInfrastructure EngineerAuthor Commented:
      Okay cool thanks l  will try another OU
      Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
      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
      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
      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.