Share via

On-premises Active directory

HASSAN BIN NASIR DAR 396 Reputation points
2022-11-16T16:01:40.057+00:00

Hi all,

Can you please provide me a script to get all users information with all attributes from Active directory and wants to save it in CSV file.

Also need to get information about OUs and Groups memberships.

thanks

Regards

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

4 answers

Sort by: Most helpful
  1. Hoekstra Jelle 501 Reputation points
    2022-11-17T15:04:17.063+00:00

    Hi!

    I just added the requested content above (members of a group). If you also need to see the dependencies (Member of section of groups) let me know.

    If it helps, please accept the answer and upvote.

    Was this answer helpful?

    0 comments No comments

  2. HASSAN BIN NASIR DAR 396 Reputation points
    2022-11-16T23:49:23.473+00:00

    Hi

    Setting group list

    $groupslist = Get-adgroup -Filter * -SearchBase "DC=test,DC=local" -Properties SamAccountName, whenCreated, ObjectClass | select SamAccountName, whenCreated, ObjectClass

    It will also extract the group members and group memberships? If not, then what parameters will be used in this script?

    Regards

    Was this answer helpful?

    0 comments No comments

  3. Hoekstra Jelle 501 Reputation points
    2022-11-16T17:58:54.093+00:00

    Hi!

    I wrote this just now, you could look into the OU using the parameter Distinguished name, I am not sure of the right cmdlet to extract the details (filtered through) to solely get the OU's

    Setting export file names

    $mainpath = "C:\temp\"
    $usersexportpath = "$mainpath\userslist.csv"
    $groupsexportpath = "$mainpath\groupslist.csv"
    $groupmemberexportpath = "$mainpath\groupmembers.csv"

    Setting user list

    $userslist = Get-ADUser -Filter * -SearchBase "DC=test,DC=local" -Properties SamAccountName,DisplayName,givenName,LastLogonDate,mail,Enabled,whenCreated, ObjectClass | select SamAccountName,DisplayName,givenName,UserPrincipalName,ProxyAddress,LastLogonDate,mail,Enabled,whenCreated, ObjectClass

    Setting group list (Specific)

    $groupslist = Get-adgroup -Filter * -SearchBase "DC=test,DC=local" -Properties SamAccountName, whenCreated, ObjectClass | select SamAccountName, whenCreated, ObjectClass

    Setting group list (overall)

    $groupslist = Get-adgroup -Filter * -SearchBase "DC=test,DC=local"

    Setting output with foreach loop to get the members of each group.

    $groupmember= foreach($group in $grouplist){
    Get-adgroup $group | get-adgroupmember
    }
    $groupmember | Export-Csv $groupmemberexportpath -encoding "unicode" -NoTypeInformation

    Writing ifstatement for users export, just solely confirming that the object class stored in $userslist matches the user type

    if($userslist.objectclass -eq "user"){
    $userslist | Export-Csv $usersexportpath -encoding "unicode" -NoTypeInformation
    }
    else{
    write-host "Something went wrong"
    }

    Writing ifstatement for groups export, just solely confirming that the object class stored in $groupslist matches the group type

    if($groupslist.objectclass -eq "group"){
    $groupslist | Export-Csv $groupsexportpath -encoding "unicode" -NoTypeInformation
    }
    else{
    write-host "Something went wrong"
    }

    Hope it helps!

    (If it does feel free to accept the answer and upvote ;) )

    For reference:

    https://learn.microsoft.com/en-us/powershell/module/activedirectory/get-adgroupmember?view=windowsserver2022-ps

    Was this answer helpful?


  4. HASSAN BIN NASIR DAR 396 Reputation points
    2022-11-16T16:36:27.223+00:00

    I m already using this script:

    Get-ADUser -Filter * -SearchBase "DC=test,DC=local" -Properties SamAccountName,DisplayName,givenName,LastLogonDate,mail,Enabled,whenCreated | select SamAccountName,DisplayName,givenName,UserPrincipalName,ProxyAddress,LastLogonDate,mail,Enabled,whenCreated | Export-Csv "C:\temp\Users.csv" -encoding "unicode" -NoTypeInformation

    This is working fine But this scripts does not retrieve the OU and group informaion. I am not femilier with Powershell. Anybody can modify it according to my requirement.

    Thanks in Advance.

    Regards

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.