Link to home
Start Free TrialLog in
Avatar of mystikal1000
mystikal1000

asked on

Powershell script - ADuser retrieving a certain group name that starts with...

I need a PS script that will read an list (1000+) user accounts and retrieve any groups that starts with "gis" including the output of the username and UPN.  I would like the output with the username, UPN and only the group name that starts with "gis".
Avatar of timgreen7077
timgreen7077

User List:
Get-ADUser -Filter * -Properties * | ft SamAccountName,UserPrincipalName


Group List:

Get-ADGroup -Filter * -Properties * | where {$_.Name -like "gis*"} | fl name
Avatar of mystikal1000

ASKER

Thanks Tim.  I would like for the powershell to read a text file of a list of user accounts that will give me an output of any group name the account is member of that starts with gis.  I would like the output to show the user account, upn and the group name that start with gis only.
$csv="C:\userlisy.csv"
$Userss = import-csv $csv
$targetOU = "OU=yourOU,DC=yourDC=your,DC=your"
Foreach($obj in $Users)
	{
	$User= $obj.Users
        Write-Host "$User"
	Get-ADUser $($obj.users)  -Properties * | ft SamAccountName,UserPrincipalName

}

Open in new window


HTH

Tom
Thanks Tom, however I don’t see where the group start with ‘gis’, output the group name.
This is using the AD Module to get all users from a certain OU and get there group name if they are part of a Group with GIS in the name.
Use can substitute the Get-ADUSER with Import-CSV  like Thomas illustrated.
$Report=@()
Get-ADUser -Filter * -searchbase ’OU=Helpdesk,DC=contoso,DC=com‘ -SearchScope OneLevel | foreach-object{

$DisplayName=$_.name
$UserPrincipalName=$_.UserPrincipalName
$groups=(Get-ADPrincipalGroupMembership –identity $_.samaccountname).name

   Foreach($group in $groups)

   {
        If($group -like '*GIS*')

            {
            $obj=new-object System.Object

            $obj |add-member -membertype NoteProperty -name "Name" -Value $DisplayName 
            $obj|add-member -membertype NoteProperty -name "User_PrincipalName" -value $UserPrincipalName
            $obj|add-member -membertype NoteProperty -name "Group_Name" -value $group

            $Report+=$obj
            }
    }
}

$Report  | Sort TotalItemSize -Descending|Export-Csv -Path C:\temp\Groups_GIS.csv -NoTypeInformation -Force

Open in new window


WIth Improt-CSV in place of Get-ADUSER.
Note: I used the standard field name from AD for this example.  Your csv file may have different column names.   If hey do please change the value to match your CSV.
$Report=@()
Import-csv -path C:\temp\Userlist.csv | foreach-object{

$DisplayName=$_.name
$UserPrincipalName=$_.UserPrincipalName
$groups=(Get-ADPrincipalGroupMembership –identity $_.samaccountname).name
   Foreach($group in $groups)

   {
        If($group -like '*GIS*')

            {
            $obj=new-object System.Object

            $obj |add-member -membertype NoteProperty -name "Name" -Value $DisplayName 
            $obj|add-member -membertype NoteProperty -name "User_PrincipalName" -value $UserPrincipalName
            $obj|add-member -membertype NoteProperty -name "Group_Name" -value $group

            $Report+=$obj
            }
    }
}

$Report  | Sort TotalItemSize -Descending|Export-Csv -Path C:\temp\Groups_GIS.csv  -NoTypeInformation -Force

Open in new window

Yo_bee - I tried your second PS and it didn't output anything.  I added my user account to the Userlist.csv file column A, cell A.

It should Output my account name, UPN and any group that starts with 'gis'.  Should I try something different?  Would the input file format be better if it was a text file?
The second one wrote to a CSV file in c:\temp.
If you do not want to output to write to the CSV then remark I with a # before | pipe
Actually I want the output to be a csv file, however it didn't show any data?  

I was talking about the input file, 'Userlist.csv'.  I put my account in the input csv file and the output file 'Groups_GIS.csv' had no data.

Import-csv -path C:\temp\Userlist.csv | foreach-object{
That has to match your environment.  You never gave any details about the file name nor the file location.
You need to edit that to match your environment.
If you post a sample of the file I can rework the script.
C:\temp folder location and file name are fine.  Here are the user accounts in this format (see attached).

I would like the output file to include all of the useraccounts, UPN, and the group name that starts with 'GIS'.

Hope this makes sense.
Attached file.
userlist.csv
Is that the only Column?
Yes I want that input file to only include the user accounts.  I want the powershell script to search for those accounts in AD and output their account name, UPN, and only the group name that starts with 'GIS'.
ASKER CERTIFIED SOLUTION
Avatar of yo_bee
yo_bee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you everyone for their help!
did it make sense?