Skip to content

Instantly share code, notes, and snippets.

@joshfinley
Created June 26, 2023 13:54
Show Gist options
  • Save joshfinley/61695fc58468f9a2a184f624a47da270 to your computer and use it in GitHub Desktop.
Save joshfinley/61695fc58468f9a2a184f624a47da270 to your computer and use it in GitHub Desktop.
Import-Module ActiveDirectory
# Specify the path to the CSV file
$csvPath = "input.csv"
# Initialize an empty array to store the results
$results = @()
# Read the CSV file and iterate over each row
$csvData = Import-Csv -Path $csvPath
foreach ($row in $csvData) {
# Retrieve the group name from the 'GroupName' column
$groupName = $row.GroupName
# Retrieve all computer objects in the group
$groupMembers = Get-ADGroupMember -Identity $groupName -Recursive |
Where-Object { $_.objectClass -eq 'computer' } |
Select-Object @{Name="GroupName"; Expression={$groupName}}, Name
# Add the group members to the results array
$results += $groupMembers
}
# Output the results to a single CSV file
$outputFilePath = "GroupComputerObjects.csv"
$results | Export-Csv -Path $outputFilePath -NoTypeInformation
Write-Host "Computer objects for all groups exported to '$outputFilePath'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment