we have custom roles in sitecore role manager and assigned them to sitecore users.

I am writing a Sitecore PowerShell script to filter the users based on my custom role 'Buyer'. below is my script. i am unable to filter users when i add the MemberOf to my script. Line #6, Line #12 not working. Please

$property = @(
@{Name='Email';Expression={ $PSItem.Profile.GetCustomProperty('email_address') }},
@{Name='FirstName';Expression={ $PSItem.Profile.GetCustomProperty('first_name') }},
@{Name='LastName';Expression={ $PSItem.Profile.GetCustomProperty('last_name') }},
@{Name='User Name';Expression={ $PSItem.Profile.GetCustomProperty('User Name') }},
@{Name='Roles';Expression={ $PSItem.MemberOf }} ########### NOT WORKING

# Gets not disabled users, next select all custome properties and save all properties to CSV file
Get-User -Filter 'Sitecore\*'  `
    | Where-Object { $_.Profile.State -ne 'Disabled' }
        `| Select-Object -ExpandProperty MemberOf ########### NOT WORKING
            `|  Select-Object -Property $property `
                | Export-CSV -Path "$apppath\sitecore-enabled-uc.csv" -notype -encoding "unicode" 
Download-File  "$apppath\Sitecore-enabled-uc.csv"

1 Answer 1


You can get the list of users in a specific role by using the Get-RoleMember command, then you can iterate through the list returned.

$users = Get-RoleMember -Identity 'Buyer'
foreach($user in $users)
    Write-Host $user.Profile.UserName

The documentation for the command https://doc.sitecorepowershell.com/appendix/security/get-rolemember

You can try running the following script to export the data:

 $Report = @()
    $ExportPath = "C:\Temp\sitecore-enabled-uc.csv"
    $items = Get-RoleMember -Identity 'Buyer'
    foreach($item in $items)
            $roles = $item.MemberOf
            $roleNames = ''
            $roles | ForEach-Object -Process { $roleNames = $roleNames + $_.Name + ' ' }

            $reportItem = [PSCustomObject]@{
                        "Email"= $item.Profile.GetCustomProperty('email_address')
                        "FirstName"= $item.Profile.GetCustomProperty('first_name')
                        "LastName"= $item.Profile.GetCustomProperty('last_name')
                        "User Name"= $item.Profile.GetCustomProperty('User Name')
                        "Roles"= $roleNames
                $Report += $reportItem
       $Report | Export-Csv -notypeinformation -Path $ExportPath -force
  • Thank you Cristi, Can you please help me to modify my script Line #6, #12. i have given the script in my question. i am generating a report.
    – Shrad
    Feb 15, 2021 at 14:49
  • I posted the modified script in the latest answer edit Feb 15, 2021 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.