SPTechCon The SharePoint and Office 365 Conference Logo

Use the Patterns and Practices PowerShell with SharePoint for fun and profit

Use the Patterns and Practices PowerShell with SharePoint for fun and profit

by: Shane Young

Okay, maybe that title is a bit of a stretch, but I wanted to make sure I got your attention. So many people skip over how important PowerShell is to their success, and I didn't want that to be you. Keep reading, and I will give you some concrete examples of how you can benefit from PowerShell and all of the resources you need to learn more.

What is Patterns and Practices PowerShell?
I am glad you asked. The Patterns and Practices team is a group of Microsoft consultant and engineers that got together to create solutions to fill the gap of customers moving from Office 365 dedicated to shared back in 2013. (We call them PNP for short.) Turns out that while they set out with a plan, the demand for their work and best practices was massive, so over the years it has evolved. They have developer tooling (yuck) and their own PowerShell cmdlets (yay) to fill some gaps. When it comes to managing SharePoint Online, their PowerShell is a must. Let me show you why.

If you don't currently use PowerShell for SharePoint Online and Office 365, you are missing the boat. To help you get on the boat, check out this article I wrote previously, or the O365 video and PNP video that guide you how to get things installed. I will assume all of that is old hat and trudge on.

Copying list items
One of the coolest things that the PNP PowerShell provides for is finally a reliable, intelligent way to move content around. For example, you can use Get-PNPListItem to get a specific item, then you can use Add-PNPListItem to create a new list item. With a little PowerShell elbow grease, you can use the properties from the Get to pass values to the Add. Finally, throw in a Remove-PNPListItem to get rid of the original, and you have made a poor man's move. Pretty cool.

At the most basic level it would look like:

$Items = Get-PNPListItem -List "Source List"

foreach ($item in $items)

{

Add-PNPListItem -List "Target List" -Values @{"Title" = $item["Title?]; "MyColumn" = $item["MyColumn?] }

}

$items | ForEach-Object {Remove-PNPListItem -List "Source List" -Identity $_.id -Force}

If you want to see that concept explained and taken a few steps further check out this video on Managing SharePoint Online List Items.

Creating content
Have you ever needed to create or copy list or libraries in SharePoint online? I have. And because I like to be as efficient (or lazy) as possible, I figured out how to use the PNP PowerShell to do so. New-PNPList will get you started. Then you can use Add-PNPField to create a column. In the PowerShell below, you will also see you can add lists to the Quick Launch bar and add columns to the default view. Boom. Automated list creation looks something like:

New-PNPList -Title "My New List? -Template "genericlist? -OnQuickLaunch

Add-PNPField -List "My New List" -type Text -DisplayName "My New Column" -InternalName "MyNewColumn" -AddToDefaultView

To see more of how you use those cmdlets check out the video (Yes, it is the same as the video from the previous section).

Moving files
With adoption for SharePoint Online going through the roof, the need to "rearrange? user content is also increasing. While moving some files around with the mouse is doable, it isn't exactly elegant and definitely is not automated. That is where PowerShell comes to the rescue. The PNP PowerShell provides for us a cmdlet called Copy-PNPFile that lets you copy from one document library to another. Then you can use Remove-PNPFile to get rid of the original effectively moving the file. Of course, if you are into simple you could also just use Move-PNPFile. Or if you want to upload a file to SharePoint, Add-PNPFile would be your friend.

This example gets all of the documents:

$source = "/docs/myfile.docx?

$target = "/newhome/myfile.docx?

Copy-PnPFile -SourceUrl $source -TargetUrl $target

Remove-PNPFile -ServerRelativeUrl $source

Or the one-step version:

Move-PnPFile -ServerRelativeUrl $source -TargetUrl $target

The sky is the limit for the fun you can have now. You just need to work out some scripting logic to make it all magical. Now for some good news and bad news. The good news is I am working on a video that will walk you through everything with automation and copying metadata. The bad news is it will not be done before the deadline for this article. So either subscribe to my channel to be notified when it publishes, or you can tweet me @ShanesCows and I will send you the link.

Learning more
The best way to learn more is to come down to SPTechCon Austin. I, along with my trusty sidekick Todd Klindt, will be teaching several classes, including a couple with PowerShell. It will be a good time. Or if you just want me to write some PowerShell for you, then you can reach out to Bold Zebras. I love PowerShell consulting engagements.

Shane is a Microsoft enthusiast from Cincinnati, Ohio, who has had the honor of being a SharePoint MVP for 12 years and counting.

View all Blog Posts
Loading