What you will find here

If you are one of those multi hat kind of person you might want to take a look here.
Some of the sections here will cover: SharePoint, SQL, Server, VB Script, Batch files, PCI Compliance

Icon

Icon

Wednesday, March 6, 2019

Add user to security group - SharePoint Online - Power Shell - Loop

To add a user to all sub sites on your hub site here the WORKING script using power shell and



Connect-SPOService –Url "YOUR ADMIN SITE HERE"

# Get all sites
Get-SPOHubSite
#SO YOU CAN GET THE ID YOU NEED


# add users to a security group
$hubSiteId= "HUB ID HERE"
$sites = Get-SPOSite -Limit ALL
foreach ($site in $sites)
{
$siteDetailed = Get-SPOSite -Detailed $site.Url
if($siteDetailed.HubSiteId -eq $hubSiteId)
{
$site.Url
$x = Get-SPOSiteGroup -Site $site.url
foreach ($y in $x)
{
if($y.Title -like "*Owners*") #OR GROUP YOU NEED
{
        $group = Get-SPOSiteGroup -Site $site.URL -Group $y.Title
        $group.title

Add-SPOUser -Site $site.url -LoginName "FULL EMAIL OF USER" -Group $group.title
}
}

}
}



No comments:

Post a Comment