This PowerShell script will let you loop through each SharePoint Online site to perform a specific action. I’ve been asked a couple of times to write a script to for example create a new document library in each site, add a web part or grant permissions. I always start with the below script so I only need to write the desired action and don’t need to worry about doing this for each site. Please let me know if you have used my script, for what purpose and if there are any questions.
Running the script
We will first start by opening the SharePoint Online Management Shell as administrator which can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=35588.
Image may be NSFW.
Clik here to view.
You will only need to add the action below the start and above the comment section and it will run for each site. Just copy and paste the full script.
function Update-SPOWeb ($web){ #Get Web information and subsites $context = New-Object Microsoft.SharePoint.Client.ClientContext($web) $context.Credentials = $SPOCredentials $web = $context.Web $context.Load($web) $context.Load($web.Webs) #send the request containing all operations to the server try{ $context.executeQuery() #check any subweb if present if ($web.Webs.Count -ne 0){ foreach ($subweb in $web.Webs){ Update-SPOWeb($subweb.url) } } ########################## ### ### ### start action ### ### ### ########################## write-host $web.title ########################## ### ### ### stop action ### ### ### ########################## } catch{ write-host "Error: $($_.Exception.Message)" -foregroundcolor green } } function Update-SPOWebs{ #variables that will be asked during the script $adminUrl = Read-Host "Please enter the Office365 Admin URL (e.g. https://spfire-admin.sharepoint.com)" $webUrl = Read-Host "Please enter the start url (e.g. https://spfire.sharepoint.com/sites/rootsite" $userName = Read-Host "Please enter the Office365 admin account (e.g. mpadmin@spfire.onmicrosoft.com)" $password = Read-Host "Please enter the password for $($userName)" -AsSecureString # set SharePoint Online and Office 365 credentials $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password) $credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $password #connect to to Office 365 try{ Connect-SPOService -Url $adminUrl -Credential $credentials write-host "Info: Connected succesfully to Office 365" -foregroundcolor green } catch{ write-host "Error: $($_.Exception.Message)" -foregroundcolor red Break delete-SPOSites } Update-SPOWeb($webUrl) } Update-SPOWebs
Image may be NSFW.
Clik here to view.
Press enter
Image may be NSFW.
Clik here to view.
Fill in the required information and press enter
Image may be NSFW.
Clik here to view.
In this case it has only listed the names for each site but you do whatever you need.
The post PowerShell script to loop through each SharePoint Online site appeared first on SharePoint Fire.