Avatar of Garry Shape
Garry Shape
Flag for United States of America asked on

Help with Powershell script that gets AD group and members' computer name based on Computer description match


This is an Active Directory environment with users, groups and computers. There is a logon script in the environment that updates computers with the username of the user logged into the computer.
Using that information, I am trying to piece together a report of certain AD Groups, and their members, along with computers that have a match of their username in their description.
So the output would show the Group name, and then the found computer object with the computer name and description. I don't need to output a username separately because that's already shown in the computer object description.

What I need help with:

  • I'd like to get it exported in a way that is a CSV. Right now it's just in the host, which is ok, but not easily shareable or to search like in Excel.
  • Just a general analysis of the script. It seems slow, maybe it's not right.

The script:

#Get all existing AD Groups
$AllADGroups = Get-ADGroup -filter * -properties Name,Description    

#Gather groups I'm targeting that share a known common prefix.  
$CertainGroups= $AllADGroups | ? {$_.Name -like "CertainPrefix*"} | Select Name,SID   

#Get all AD Computers that we will check against once we have usernames.     
$ADComputers = get-adcomputer -filter * -properties Name,Description

#iterate through the groups and come up with the report
foreach ($Group in $CertainGroups) {

$Name = $Group.Name
$GroupMembers = get-adgroupmember $Group.SID | select -expand samaccountname
write-host $Name -foregroundcolor "Black" -backgroundcolor "White"

$MemberComputer = foreach ($member in $GroupMembers) { $ADComputers | ? {$_.Description -like "$member*"} }
$MemberComputer | select Name,Description

Open in new window

This then outputs something like in the host, but ideally put to Excel/CSV if there's a way:  
Group Name Here
laptop01 john.doe (other info in computer description )
laptop02 pat.doe (other info in computer description )
desktop01 jane.doe (other info in computer description )
PowershellActive Directory

Avatar of undefined
Last Comment
Garry Shape

8/22/2022 - Mon

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Garry Shape

Bro brilliant! Thanks so much. Do you do paid services here? Love to work with you to help me more.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy