Last Update: Sep 04, 2024 | Published: Dec 18, 2017
PowerShell is not only a powerful tool to administer and manage a SharePoint Online (SPO) tenant but also for common activities as an Office 365 Administrator or an SPO. In this article, I will cover some of the most common PowerShell for SharePoint Online usage scenarios as described in Figure 1.
Figure 1– Common PowerShell for SPO Usage Scenarios.
Under these scenarios, we have any action that implies to apply specific SPO settings available through SPO PowerShell cmdlets and/or SPO APIs. Some examples of typical operations that fall under these scenarios are the following ones:
Set-SPOTenant -SpecialCharactersStateInFileFolderNames Allowed $O365SPOTenant=Get-SPOTenant $O365SPOTenant.SpecialCharactersStateInFileFolderNames
$sO365GroupSite="https://<Your_Group_Site_Url>" Set-SPOSite -Identity $sO365GroupSite -SharingCapability ExternalUserSharingOnly
On the one hand, Auditing Operations scenario is intended to provide information about what is happening at any logical containers in an SPO tenant (Site Collections, Sites, Lists, Document Libraries, etc) in regards to common operations, such as creating or updating content, making updates in SPO security model and so on. On the other hand, reporting generation scenario is about activities taking place in SPO that are also covered in this PowerShell usage scenario. Some good examples of these scenarios:
For instance, if you are asked to provide a report with all the members of each SharePoint Security Group configured on an SPO site, you only need to execute the following PowerShell script that uses SPO Get-SPOSiteGroup and Get-SPOUser cmdlets:
$spoSharePointGroups=Get-SPOSiteGroup -Site $sSiteUrl foreach($spoSharePointGroup in $spoSharePointGroups){ Write-Host "Users in " $spoSharePointGroup.Title ":" $spoUsers=Get-SPOUser -Site $sSiteUrl -Group $spoSharePointGroup.Title Write-Host “ -> “ $spoUsers.LoginName Write-Host “--------------------------------“ -ForegroundColor Green }
For instance, to query the Office 365 audit log and get information about file activities happening in all the sites in the tenant simply execute the following PowerShell script:
$PSSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic -AllowRedirection Import-PSSession $PSSession Search-UnifiedAuditLog -StartDate 12/1/2017 -EndDate 12/7/2017 -RecordType SharePointFileOperation -Operations FileAccessed -SessionId "Docs_SharepointViews"-SessionCommand ReturnNextPreviewPage
PowerShell is a common vehicle to deploy solutions on top of SPO that also includes any kind of customization to new or existing SPO Sites. Under this scenario, we can find a wide range of possibilities:
As an example, you can create a new SPO list in an SPO site using the following PowerShell script that makes use of the client-side object model (CSOM) SPO API:
#Adding the Client OM Assemblies $sCSOMRuntimePath=$sCSOMPath + "Microsoft.SharePoint.Client.Runtime.dll" $sCSOMPath=$sCSOMPath + "Microsoft.SharePoint.Client.dll" Add-Type -Path $sCSOMPath Add-Type -Path $sCSOMRuntimePath #SPO Client Object Model Context $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword) $spoCtx.Credentials = $spoCredentials #Creating the List $spoWeb=$spoCtx.Web $spoListCreationInformation=New-Object Microsoft.SharePoint.Client.ListCreationInformation $spoListCreationInformation.Title=$sListName $spoListCreationInformation.TemplateType=[int][Microsoft.SharePoint.Client.ListTemplatetype]::GenericList $spoList=$spoWeb.Lists.Add($spoListCreationInformation) $spoList.Description=$sListDescription $spoCtx.ExecuteQuery() $spoCtx.Dispose()
Finally, last scenarios cover situations where it’s required either to upload data to SPO sites or to move/migrate information to SPO sites. Make note that this information could come from another SPO Site or event SPO tenant, from a SharePoint OnPremises farm or even from a corporate file server. Some examples of situations that are under these scenarios are the following:
For instance, the following PowerShell script allows to upload information from a CSV file to an SPO list using SPO CSOM API:
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword) $spoCtx.Credentials = $spoCredentials #Adding Data to an existing list $spoList = $spoCtx.Web.Lists.GetByTitle($sListName) $spoCtx.Load($spoList) foreach ($sItem in $tblItems) { Write-Host "Adding " $sItem.SPOListItem " to $sListName" $spoListItemCreationInformation = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation $spoListItem=$spoList.AddItem($spoListItemCreationInformation) $spoListItem["Title"]=$sItem.SPOListItem.ToString() $spoListItem.Update() $spoCtx.ExecuteQuery() } $spoCtx.Dispose()
PowerShell for SPO is a tool not only for platform administration and configuration tasks but also for doing many other common activities as an SPO Administrator (or an Office 365 one) can require: Auditing Operations, Reporting, SPO Solutions Deployment, Data Loading, and Migration.