Skip to content
The Lazy Administrator
  • Home
  • Disclaimer
  • Contact
  • About Me
  • Search Icon

The Lazy Administrator

Finding ways to do the most work with the least effort possible

Manage and Administer SharePoint using SharePointPnP.PowerShell

Manage and Administer SharePoint using SharePointPnP.PowerShell

August 20, 2018 Brad Wyatt Comments 1 comment

Table of Contents

  • Connecting to SharePoint
  • Enterprise Keywords
    • Term Store
    • Upload Files to SharePoint
    • Tag Existing Data
      • One Keyword
      • Multiple Keywords
  • Copy Files Between Sites & Site Collections
  • Sources

The SharePoint Development Community (also known as the SharePoint PnP community) is an open-source initiative coordinated by SharePoint engineering. This community controls SharePoint development documentation, samples, reusable controls, and other relevant open-source initiatives related to SharePoint development1 .

The SharePointPnP.PowerShell module is quite vast when it comes to managing and administrating your on-premise or SharePoint online environment. Before we dive into it you will first want to install it so you have all the cmdlets available to you.

SharePoint Version Command to install
SharePoint Online Install-Module SharePointPnPPowerShellOnline
SharePoint 2016 Install-Module SharePointPnPPowerShell2016
SharePoint 2013 Install-Module SharePointPnPPowerShell2013

Note: This will be an on-going article. As I continue using this module I will update this article.


Connecting to SharePoint

In my environment I have SharePoint with Office 365 so I will be connecting to SharePoint Online. For testing purposes I will be working in one of my test sites so I will connect directly to that site.

$webUrl = "https://bwya77.sharepoint.com/sites/o365_training/"
Connect-PnPOnline -Url $webUrl

Running Connect-PnPOnline will prompt me for credentials. Once I enter my administrator credentials I am connected in seconds.

Enterprise Keywords

Users add enterprise keywords to items on a SharePoint site to use for tagging and to develop a folksonomy. Enterprise keywords can capture some of the knowledge of the people who use the content. To make it easier to add keywords, you can add a special enterprise keywords column to a list or library. Users then can select the item to add a keyword to, and enter the word or phrase they want in the item properties2 .

Term Store

When working with Enterprise Keywords I found that I had to specify the keyword by using its GUID from the SharePoint Term Store. This may change in future versions, at the time of this post I am working with 3.0.1808.1.

To find the GUID of your term navigate to the SharePoint Admin Center and select “Term Store” on the left pane.

In my script I store the GUIDs in variables so I can reference them easier.

#Enterprise Keywords GUIDs
$Test = "5b6ddded-ce24-4e39-a698-14b67ab282e0"
$Dumb = "68a89466-cdaa-4922-9120-98d8d98305ce"

Upload Files to SharePoint

In my example I want to upload every file in C:\Transfer\Upload as well as all files in any sub-folders. If I did not want to include files in sub-folders I would remove the -Recurse parameter.

In SharePoint Online I want to add all of the local files to a folder called “Training”. Remember we connected to site URL https://bwya77.sharepoint.com/sites/o365_training/ so really all files will be at the URL https://bwya77.sharepoint.com/sites/o365_training/Training/

I can see this folder as well on my left navigation pane

During this upload I also want to tag the “Test” Enterprise Keyword to each file. To do this I use the -Values parameter and then call the TaxKeyword value.

-Values @{ "TaxKeyword" = $Test, $Test }

I put the variable there twice because it will set the taxValueCollection and then the TaxKeyword. If you put it in only once it will error on a null value.

#Upload files in Dir and add Enterprise keywords
$Files = Get-ChildItem -Path C:\Transfer\Upload -Force -Recurse
foreach ($File in $Files)
{
	
	write-host "Uploading $($File.Directory)\$($File.Name)"
	
	#Its in there twice because we must set taxValueCollection
	Add-PnPFile -Path "$($File.Directory)\$($File.Name)" -Folder "/Training/" -Values @{ "TaxKeyword" = $Test, $Test }
	
}

Tag Existing Data

The next item I want to achieve is to tag all existing files with new Enterprise Keywords. I will be working in the same site and in the same folder.

One Keyword

If I want to only tag the data with the “Dumb” Enterprise Keyword I can run the following

$folderurl = "https://bwya77.sharepoint.com/sites/o365_training/Training"


$folder = Get-PnPFolder -RelativeUrl $folderUrl
$files = Get-PnPProperty -ClientObject $folder -Property Files
foreach ($File in $Files)
{
	
	write-host "Working on $($file.name)"
	$item = Get-PnPFile -Url "/Training/$($file.name)" -AsListItem
	#Its in there twice because we must set taxValueCollection
	Set-PnPListItem -List "Training" -Identity $item.ID -Values @{ "TaxKeyword" = $Dumb, $Dumb }
	
	
}
Multiple Keywords

If I want to tag the files with the “Test” and “Dumb” Enterprise Keywords, I can run the following:

$folderurl = "https://bwya77.sharepoint.com/sites/o365_training/Training"


$folder = Get-PnPFolder -RelativeUrl $folderUrl
$files = Get-PnPProperty -ClientObject $folder -Property Files
foreach ($File in $Files)
{
	
	write-host "Working on $($file.name)"
	$item = Get-PnPFile -Url "/Training/$($file.name)" -AsListItem
	#Its in there twice because we must set taxValueCollection
	Set-PnPListItem -List "Training" -Identity $item.ID -Values @{ "TaxKeyword" = $Test,$Test,$Dumb,$Dumb }
	
	
}

Copy Files Between Sites & Site Collections

Next I want to copy all files and folders (including the files and folders nested within) from one site or site collection, to another. You can also copy everything from document libraries, folders and more.

In my example I will be copying everything from https://bwya77.sharepoint.com/sites/o365_training/Training to another site called Files and into the Shared Documents folder.

$webUrl = "https://bwya77.sharepoint.com/sites/o365_training/"
Connect-PnPOnline -Url $webUrl

$folderurl = "https://bwya77.sharepoint.com/sites/o365_training/Training"

$folder = Get-PnPFolder -Url $folderUrl
$files = Get-PnPProperty -ClientObject $folder -Property Files
foreach ($File in $Files)
{
	Copy-PnPFile -SourceUrl "$folderurl" -TargetUrl "/sites/Files/Shared Documents/" -Force -OverwriteIfAlreadyExists
}

Here we see that it automatically created a folder called Training and inside the folder is everything from the source.

We can also see the folders and files that were at the source. The folder structure came over exactly the same. Every file from the source will be in the same spot in the destination.

 

 


Sources

1: https://docs.microsoft.com/en-us/sharepoint/dev/community/community

2: https://support.office.com/en-us/article/add-an-enterprise-keywords-column-to-a-list-or-library-314ce556-e4bf-4ef7-9939-6a1bedfc434a

Brad Wyatt
Brad Wyatt

My name is Bradley Wyatt; I am a 5x Microsoft Most Valuable Professional (MVP) in Microsoft Azure and Microsoft 365. I have given talks at many different conferences, user groups, and companies throughout the United States, ranging from PowerShell to DevOps Security best practices, and I am the 2022 North American Outstanding Contribution to the Microsoft Community winner.


PowerShell, SharePoint
Keywords, MetaData, Office 365, PowerShell, SharePoint

Post navigation

PREVIOUS
[Tool] Create and Configure Active Directory and Office 365 Users at Once.
NEXT
Customize your Office 365 Encrypted Messages with your Organizations Brand in Office 365

One thought on “Manage and Administer SharePoint using SharePointPnP.PowerShell”

  1. Maciej says:
    December 21, 2018 at 5:15 am

    You saved my day with this: -Values @{ “TaxKeyword” = $Test, $Test }

    Thank you!!!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Subscribe

Email


Categories

  • Active Directory (8)
  • AI (3)
  • API (1)
  • AutoPilot (2)
  • Azure (15)
  • Bicep (4)
  • Connectwise (1)
  • Defender for Cloud Apps (1)
  • Delegated Admin (1)
  • DevOps (6)
  • Graph (6)
  • Intune (15)
  • LabTech (1)
  • Microsoft Teams (6)
  • Office 365 (19)
  • Permissions (2)
  • PowerShell (50)
  • Security (1)
  • SharePoint (3)
  • Skype for Business (1)
  • Terraform (1)
  • Uncategorized (2)
  • Yammer (1)

Recent Comments

  • MD SHARIQUE AKHTAR on Modern Active Directory – An update to PSHTML-AD-Report
  • TommyBoich on How The ConnectWise Manage API Handles Pagination with PowerShell
  • LOTTERY 365 LOGIN on Windows LAPS Management, Configuration and Troubleshooting Using Microsoft Intune
  • SPRUNKI PHASE 6 on Get a New Computer’s Auto Pilot Hash Without Going Through the Out of Box Experience (OOBE)
  • Mohammad Sherbaji on Get a New Computer’s Auto Pilot Hash Without Going Through the Out of Box Experience (OOBE)

1,738,849 People Reached

© 2025   All Rights Reserved.