troubleshooting Question

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

Avatar of garryshape
garryshape asked on
PowershellActive Directory
2 Comments1 Solution77 ViewsLast Modified:

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
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 )
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros