78.1k views
0 votes
Write out the PowerShell command to perform the following:

1. Find all AD groups without regard for any criteria
2. Check to see if a single AD group called HR exists
3. List all members of the Administrators group
4. List all groups nested inside the HR group
5. List all group members of the HR, Accounting, and IT groups using a foreach loop
6. Check to see if a single AD group called HR exists, but have the command prompt you for alternate username/password
7. List all the group members of the HR group, along with each user account’s email address
8. List only the Security groups
9. Assume you have a "Locations" organizational unit (OU) at the root of your AD, and you have three OUs underneath it: Houston, NYC, and LA. List all AD groups that are only in the "NYC" OU.
10.Perform the previous action, but export the results to a CSV file called "nyc.csv."

1 Answer

4 votes

Final answer:

To manage AD groups with PowerShell, you use the 'Get-ADGroup', 'Get-ADGroupMember', and 'Get-ADUser' commands with various parameters to list all groups, verify the existence of specific groups, list members, include properties like email, filter by group category, and manage with alternate credentials.

Step-by-step explanation:

To perform various Active Directory (AD) tasks using PowerShell, here are the respective commands:

  1. Find all AD groups without regard for any criteria:
    Get-ADGroup -Filter *
  2. Check to see if a single AD group called HR exists:
    Get-ADGroup -Identity HR
  3. List all members of the Administrators group:
    Get-ADGroupMember -Identity 'Administrators'
  4. List all groups nested inside the HR group:
    Get-ADGroup -Filter {Name -like "HR"} | Get-ADGroupMember | Where-Object { $_.objectClass -eq 'group' }
  5. List all group members of the HR, Accounting, and IT groups using a foreach loop:
    $groups = 'HR','Accounting','IT'
    foreach ($group in $groups) {
    Get-ADGroupMember -Identity $group
    }
  6. Check to see if an AD group called HR exists, prompting for alternate username/password:
    $cred = Get-Credential
    Get-ADGroup -Identity HR -Credential $cred
  7. List all the group members of the HR group with user account’s email address:
    Get-ADGroupMember -Identity HR | Get-ADUser -Properties EmailAddress | Select-Object Name,EmailAddress
  8. List only the Security groups:
    Get-ADGroup -Filter 'GroupCategory -eq "Security"'
  9. List all AD groups in the "NYC" OU:
    Get-ADGroup -Filter * -SearchBase "OU=NYC,OU=Locations,DC=YourDomain,DC=com"
  10. Export all AD groups in the "NYC" OU to a CSV file:
    Get-ADGroup -Filter * -SearchBase "OU=NYC,OU=Locations,DC=YourDomain,DC=com" | Export-Csv -Path "nyc.csv" -NoTypeInformation

User Bryan Oemar
by
8.0k points