We help IT Professionals succeed at work.
Get Started

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

garryshape asked
Last Modified: 2020-02-19

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 )
Watch Question
Most Valuable Expert 2019
Most Valuable Expert 2018
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE