Skip to content

Instantly share code, notes, and snippets.

@OlafD
OlafD / FormatMapImage.html
Created August 29, 2019 06:56
Use an image from formatmap32x32.png
<span id="csvViewsMonthsForPagesInSiteUrl" class="ms-cui-img-32by32 ms-cui-img-cont-float" onclick='triggerCsvSearch("ViewsMonthsSitePages");'>
<img src="/_layouts/15/1033/images/formatmap32x32.png" style="top: -69px; left: -511px" />
</span>
@OlafD
OlafD / GetNameOfReaderRole.ps1
Created October 25, 2019 08:58
For multilingual environments, a short version to get the name of the reader role in SharePoint Online. PowerShell PnP is needed for this approach.
$web = Get-PnPWeb -Includes RoleDefinitions
$readerRole = $roleDefinitions.GetByType([Microsoft.SharePoint.Client.RoleType]::Reader)
(Get-PnPContext).Load($readerRole)
Invoke-PnPQuery
$readerRole.Name
@OlafD
OlafD / IndexOfNth.psq1
Created January 17, 2020 13:08
Find the index of the n-th occurence of a pattern (or a single character) in a string.
<#
Find the n-th occurence of a pattern in a string
#>
function IndexOfNth
{
param (
[string]$String,
[string]$Pattern,
[int]$Occurence
@OlafD
OlafD / SetTokenLifetimeInAzure.ps1
Last active April 15, 2020 11:26
Set the access token lifetime in Azure AD for the whole organization. In the script it will be set to 10min, the minimum possible. Needs the module Azure AD PowerShell 2.0, To run connect with Connect-AzureAD to your directory.
$policy = New-AzureADPolicy `
-Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"00:10:00","MaxAgeSessionSingleFactor":"00:10:00"}}') `
-DisplayName "WebPolicyScenario" `
-IsOrganizationDefault $true `
-Type "TokenLifetimePolicy"
@OlafD
OlafD / SharePointMakeArticlePage.ps1
Created April 17, 2020 12:21
Change the site type of a page in the Site Pages library in SharePoint Online from a News page to an Article page.
$item = Get-PnPListItem -List "Site Pages" -Id 11 # use the id of your page
$item["PromotedState"] = 0
$item.Update()
Invoke-PnPQuery
@OlafD
OlafD / SharePointMakeNewsPage.ps1
Created April 17, 2020 12:23
Change the site type of a page in the Site Pages library in SharePoint Online from an Article page to an unpublished News page.
$item = Get-PnPListItem -List "Site Pages" -Id 11 # use the id of your page
$item["PromotedState"] = 1
$item.Update()
Invoke-PnPQuery
@OlafD
OlafD / SPOWithRest.ps1
Created May 8, 2020 13:15
Short example on how to use an app registration to call rest services for SharePoint Online from PowerShell.
# set variables
$clientId = "{id of your registered app}"
$clientSecret = "{secret of your registered app}"
$realm = "{id of the tenant}" # could be found in the registered app in Azure Active Directory
$principal = "00000003-0000-0ff1-ce00-000000000000" # this is the SharePoint resource
$targetHost = "{SharePoint root of the tenant}" # example: mytenant.sharepoint.com
# url used for the rest call later to test the access
@OlafD
OlafD / CreateSiteAssetsLibrary.ps1
Created June 18, 2020 14:58
In a SharePoint Online site with a Communication Site template, the Site Assets library is not created by default. It will automatically added, when a user uploads images or changes the site logo. This script will create the Site Assets library, when it is necessary to have it during other processes after site creation.
Connect-PnPOnline -Url {your_site_url}
$web = Get-PnPWeb
$list = $web.Lists.EnsureSiteAssetsLibrary()
(Get-PnPContext).Load($list)
Invoke-PnPQuery
@OlafD
OlafD / LoopSubwebs.ps1
Created July 31, 2020 08:15
Loop all subwebs of a site collection (root site). Needs the PowerShell PnP extensions for SharePoint Online.
param (
[string]$Url,
$Credentials
)
function LoopWebs
{
param (
$WebCollection
)
@OlafD
OlafD / TranscriptSample.ps1
Created July 31, 2020 08:47
Sample for storing a transcript of a PowerShell script execution to a file. The path for the transcript file is a mandatory parameter in the script.
param (
[Parameter(Mandatory=$true)]
[string]$TranscriptPath
)
$scriptName = $MyInvocation.MyCommand.Name.Substring(0, $MyInvocation.MyCommand.Name.IndexOf("."))
$transcriptExtension = Get-Date -Format yyyyMMdd-HHmmss
$transcriptFile = "$TranscriptPath\$scriptName" + "_Transcript_$transcriptExtension.txt"