I have a very large and complex SharePoint 2016 site with hundreds of groups.
I write a PowerShell script to add a specific user to a group if the group name matches text.
For example, I need to add UserX to any group that has the word "English" in the group name.
I have the following code that works to add the user to an individual group, but I do not know how to loop and add them only when the partial text matches.
$ site = get-spsite https: // site #get web object $ web = $ site.RootWeb #Enumarate groups of sites $ web.SiteGroups | select the name #get user object $ user = $ web.EnsureUser ("domain user") #Get a group object $ group = $ web.SiteGroups["Year 10 ENGLISH Staff"] #add a user to a group $ group.AddUser ($ user)
Thank you for any help