Avatar of Triforce2000
Triforce2000Flag for Belgium

asked on 

Powershell list users with filtered groups

I want to list all users with filtered AD groups, so I want all groups beginning with BLOG.
I've got a CSV with the name of all users.

I've tryed this code:
$users = Import-Csv 'C:\temp\old farm\users.csv' -Delimiter ";"

$Report = @()

foreach ($user in $users){
    $name  = $user.name

    $gebruiker = get-aduser -Filter {(displayname -like $name) -AND (enabled -eq $true)} -Properties *
    $sam = $gebruiker.SamAccountName
    $usergroupcollection = Get-ADPrincipalGroupMembership $sam | ? {$_.name -like "blog*"}
    
   
    
    $usergroupmembership = @()

    foreach ($UserGroup in $usergroupcollection) {
        $GroupDetails = Get-ADGroup -Identity $UserGroup
        $usergroupmembership += $GroupDetails.Name
    }

    $groups = $usergroupmembership -join ','

    $out = New-Object PSObject
    $out | Add-Member -MemberType NoteProperty -Name Name -Value $gebruiker.Name
    $out | Add-Member -MemberType NoteProperty -Name Sam -Value $gebruiker.SamAccountName
    $out | Add-Member -MemberType NoteProperty -Name Groups -Value $groups

    $report += $out
}

$Report | Sort-Object Name | FT -AutoSize

Open in new window


But there is an error on this
User generated image
Has anyone a solution for this please?
Powershell

Avatar of undefined
Last Comment
Chirag Nagrekar
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
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
What do you get when you print $sam ?
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo