Skip to content

PnP Powershell

Information

References

Security / Authentication

Tools / Plugin

Login with an Application admin

-The required Office 365 role to run PnP Powershell with Scopes

Installation Options

Documentation

Specific Version

Latest Version

Check

Get-InstalledModule | foreach { $b = (find-module $_.name).version ; if ($b -ne $_.version) { Write-host "$($_.name) has an update from $($_.version) to $b" } }
Get-Module SharePointPnPPowerShell* -ListAvailable | Select-Object Name,Version | Sort-Object Version -Descending

Update Module to the latest version

Update-Module SharePointPnPPowerShell*

Delete the old version

Get-InstalledModule -Name "SharePointPnPPowerShellOnline" -RequiredVersion 3.8.1904.0 | Uninstall-Module

Install a specific version

Install-Module -Name SharePointPnPPowerShellOnline -RequiredVersion 3.0.1808.1

Connect with App Permission

Initialize-PnPPowerShellAuthentication -ApplicationName DemoApp -Tenant tenant.onmicrosoft.com -Store CurrentUser

$url = "https://tenant.sharepoint.com"
$clientid = "<placeholder>"
$thumbprint = "<placeholder>"
$tenant = 'tenant.onmicrosoft.com'

Connect-PnPOnline -Url $url -ClientId $clientid -Thumbprint $thumbprint -Tenant $tenant
# Load PNP the Right Way
$pnp = Get-Command Connect-Stuff -ErrorAction SilentlyContinue
if (!$pnp) {Install-Module SharePointPnPPowerShellOnline -Force}
Import-Module SharePointPnPPowerShellOnline

List all commands

Get-Command | ? { $_.ModuleName -eq "SharePointPnPPowerShellOnline" }

Create Guid

[guid]::NewGuid() | Select-Object -ExpandProperty Guid | clip

Delete all listitems

Get-PnPList -Identity Lists/MyList | Get-PnPListItem -PageSize 100 -ScriptBlock { Param($items) 
$items.Context.ExecuteQuery() } | % {$_.DeleteObject()}

Upload Documents

function UploadDocuments(){
Param(
        [ValidateScript({If(Test-Path $_){$true}else{Throw "Invalid path given: $_"}})] 
        $LocalFolderLocation,
        [String] 
        $siteUrl,
        [String]
        $documentLibraryName
)
Process{
        $path = $LocalFolderLocation.TrimEnd('\')

        Write-Host "Provided Site :"$siteUrl -ForegroundColor Green
        Write-Host "Provided Path :"$path -ForegroundColor Green
        Write-Host "Provided Document Library name :"$documentLibraryName -ForegroundColor Green

          try{
                $credentials = Get-Credential

                Connect-PnPOnline -Url $siteUrl -CreateDrive -Credentials $credentials

                $file = Get-ChildItem -Path $LocalFolderLocation -Recurse
                $i = 0;
                Write-Host "Uploading documents to Site.." -ForegroundColor Cyan
                (dir $path -Recurse) | %{
                    try{
                        $i++
                        if($_.GetType().Name -eq "FileInfo"){
                          $SPFolderName =  $documentLibraryName + $_.DirectoryName.Substring($path.Length);
                          $status = "Uploading Files :'" + $_.Name + "' to Location :" + $SPFolderName
                          Write-Progress -activity "Uploading Documents.." -status $status -PercentComplete (($i / $file.length)  * 100)
                          $te = Add-PnPFile -Path $_.FullName -Folder $SPFolderName
                         }          
                        }
                    catch{
                    }
                 }
            }
            catch{
             Write-Host $_.Exception.Message -ForegroundColor Red
            }

  }
}


#UploadDocuments -LocalFolderLocation {Local Folder Location} -siteUrl {Site collection URL} -documentLibraryName {Document Library Name}

Site Classification

Connect-PnPOnline -Scopes "Directory.ReadWrite.All"
Enable-PnPSiteClassification -Classifications "HBI","LBI","Top Secret" -UsageGuidelinesUrl ```
"http://aka.ms/sppnp" -DefaultClassification "HBI"
Add-PnPSiteClassification -Classifications "SBI","MBI"
Remove-PnPSiteClassification -Classifications "SBI"
Update-PnPSiteClassification -Classifications "HBI","LBI","Top Secret" -UsageGuidelinesUrl http://aka.ms/sppnp" -DefaultClassification "HBI"
Disable-PnPSiteClassification

Tips

Documents List

List Properties

Connect-PnPOnline -Url https://toddklindt.sharepoint.com/sites/8884aced -Credentials Me
Get-PnPView -List Documents
Get-PnPView -List Documents -Identity 3c4126aa-d2fe-4b57-9a70-e03ebb9c76ef
$view = Get-PnPView -List Documents -Identity 3c4126aa-d2fe-4b57-9a70-e03ebb9c76ef
$view
$view | select *
$view.ViewQuery
Get-PnPProperty -ClientObject $view -Property ViewQuery
$view.ViewQuery
$view
$view | select *

Fields

Copy-List

Get HubSiteId and associated Sites

```Powershell $AdminCenterURL="https://contoso-admin.sharepoint.com" $hubSiteUrl = "https://contoso.sharepoint.com"

Connect-PnPOnline $AdminCenterURL -Interactive $adminConnection = Get-PnPConnection

$HubSiteID = (Get-PnPTenantSite -Identity $hubSiteUrl -Connection $adminConnection ).HubSiteId

Get associated sites with hub

$associatedSites = Get-PnPTenantSite -Detailed -Connection $adminConnection | Where-Object { $_.HubSiteId -eq $HubSiteID }

$associatedSites

```Powershell