Happy new year everyone!! A few days before the end of the year I published a new script on TechNet Gallery about how to populate a SharePoint group reading from User Profiles using Windows Powershell.
I chose to read from User Profiles instead of querying Active Directory because user profiles could have more information than AD since we could add a synchronization connection that reads from BCS or other sources.
How it works? The script is quite easy. It accepts four parameters: Url (the web address of the SPWeb that host the group), GroupName (the name of the group to populate, i.e. Contoso Visitors), Filter (the user profile property to filter, i.e. Department), FilterValue (the value of the field to filter for, i.e. Legal). You can call the script this way from your favorite shell or you can also choose to create a Scheduled Task to execute the script on a regular basis.
& '.\Populate-SharePoint-Group-from-User-Profile.ps1' -Url "http://intranet/legal/agreements" -GroupName "Readers of agreements" -Filter "Department" -FilterValue "Legal"
Please note that every time the script runs it removes all users from the group and then add them again (I know, this could be better). At the moment there is no error handling and logging except while adding users to group. This is something I’d like to improve in the future.
You can read more on TechNet Gallery: