{"id":65,"date":"2024-03-27T07:09:19","date_gmt":"2024-03-27T07:09:19","guid":{"rendered":"https:\/\/myallcodes.in\/?p=65"},"modified":"2024-03-27T08:08:39","modified_gmt":"2024-03-27T08:08:39","slug":"to-get-the-all-groups-and-members-details-from-active-directory-using-power-shell-script-and-output-will-be-store-in-csv-file","status":"publish","type":"post","link":"https:\/\/myallcodes.in\/index.php\/2024\/03\/27\/to-get-the-all-groups-and-members-details-from-active-directory-using-power-shell-script-and-output-will-be-store-in-csv-file\/","title":{"rendered":"To get the all groups and members details from Active directory using Power Shell script and output will be store in csv file"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>mport-Module ServerManager\nAdd-WindowsFeature RSAT-AD-PowerShell\n\nImport the Active Directory module\n\nImport-Module ActiveDirectory\n\nSpecify the distinguished name of your domain\n\n$domainDN = \"DC=test,DC=com\"\n\nCreate an array to store results\n\n$results = @()\n\nGet all groups in Active Directory\n\n$groups = Get-ADGroup -Filter *\n\nCheck if any groups are found\n\nif ($groups.Count -eq 0) {\nWrite-Host \"No groups found in Active Directory.\"\n}\nelse {\n# Iterate through each group and get its members\nforeach ($group in $groups) {\n$groupName = $group.SamAccountName\n$groupInfo = New-Object PSObject -Property @{\n'GroupName' = $groupName\n'Members' = \"\"\n}\n\n    Write-Host \"Processing Group: $groupName\"\n\n    # Get group members using the Active Directory module\n    $groupMembers = Get-ADGroupMember -Identity $groupName\n\n    # Check if any members are found\n    if ($groupMembers.Count -eq 0) {\n        Write-Host \"  No members found for group: $groupName\"\n    }\n    else {\n        $memberNames = $groupMembers | ForEach-Object {\n            $_.SamAccountName + \" (\" + $_.Name + \")\"\n        }\n\n        $groupInfo.Members = $memberNames -join ', '\n        Write-Host \"  Members: $($groupInfo.Members)\"\n    }\n\n    # Add the group information to the results array\n    $results += $groupInfo\n}\n\n# Export the results to a CSV file\n$csvPath = \"C:\\temp\\OutputFile.csv\"\n$results | Export-Csv -Path $csvPath -NoTypeInformation\n\nWrite-Host \"Script Execution Completed. CSV file has been created at: $csvPath\"\n\n}\n\nGet-ADGroupMember -Identity \"Domain Admins\" | Select-Object SamAccountName,Name | Export-Csv -Path \"C:\\Temp\\OutputFile.csv\" -NoTypeInformation<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-power-shell-scripts"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/myallcodes.in\/index.php\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myallcodes.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myallcodes.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myallcodes.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myallcodes.in\/index.php\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":3,"href":"https:\/\/myallcodes.in\/index.php\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":72,"href":"https:\/\/myallcodes.in\/index.php\/wp-json\/wp\/v2\/posts\/65\/revisions\/72"}],"wp:attachment":[{"href":"https:\/\/myallcodes.in\/index.php\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myallcodes.in\/index.php\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myallcodes.in\/index.php\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}