Skip to content

Instantly share code, notes, and snippets.

@dhcgn
Last active July 23, 2024 12:38
Show Gist options
  • Save dhcgn/ae3c64786efdcc644fbd29e20482d74a to your computer and use it in GitHub Desktop.
Save dhcgn/ae3c64786efdcc644fbd29e20482d74a to your computer and use it in GitHub Desktop.
This is a collection of PowerShell commands for working with Active Directory groups. These commands can be used to retrieve information about groups, filter groups based on certain criteria, and perform other tasks. The commands are written in PowerShell, a scripting language commonly used for automating administrative tasks in Windows environm…

AD Groups handling without dependencies

Introduction

This is a collection of PowerShell commands for working with Active Directory groups. These commands can be used to retrieve information about groups, filter groups based on certain criteria, and perform other tasks. The commands are written in PowerShell, a scripting language commonly used for automating administrative tasks in Windows environments.

Used Commands

Here's a breakdown of the commands used in the README:

  1. whoami

    • A built-in Windows command
    • Displays information about the current user
    • Parameter /groups: Shows group membership information
    • Parameter /FO CSV: Formats the output as CSV (Comma-Separated Values)
  2. ConvertFrom-Csv

    • A PowerShell cmdlet
    • Converts CSV strings into custom objects
  3. ?{ }

    • PowerShell alias for Where-Object
    • Filters objects based on specified criteria
  4. -like

    • PowerShell comparison operator
    • Performs wildcard string matching
  5. Group-Object

    • PowerShell cmdlet
    • Groups objects based on a specified property
    • Parameter -NoElement: Omits the group members from the output
  6. %{ }

    • PowerShell alias for ForEach-Object
    • Performs an operation on each item in a collection
  7. Sort-Object

    • PowerShell cmdlet
    • Sorts objects based on specified properties
    • Parameter -Descending: Sorts in descending order
  8. Select-Object

    • PowerShell cmdlet
    • Selects specified properties of objects
    • Parameter -First: Selects the first n number of objects
  9. |

    • PowerShell pipeline operator
    • Passes the output of one command as input to the next command

These commands are combined to retrieve group information, filter results, perform string operations, group and sort data, and select specific output, allowing for various manipulations of Active Directory group data.

Disclaimer

Please note that the column name Gruppenname used in these examples is based on a German language Windows system. You may need to change this to match the language of your Windows system. To determine the correct column name for your system, run the following command and check the output:

whoami /groups /FO CSV | ConvertFrom-Csv | Get-Member -MemberType NoteProperty

Get All Groups

whoami /groups /FO CSV | ConvertFrom-Csv

Get All Groups with RDP in the name

whoami /groups /FO CSV | ConvertFrom-Csv | ?{$_.Gruppenname -like "*RDP*"}

Get count of each group type

whoami /groups /FO CSV | ConvertFrom-Csv | Group-Object Typ -NoElement

List Top 25 Most Common Group Name Components

whoami /groups /FO CSV | ConvertFrom-Csv | %{$_.Gruppenname} | ?{$_ -like "*\*"} | %{$_.Split("\")[1].Split("_")} | Group-Object -NoElement | Sort-Object Count -Descending | Select-Object -First 25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment