Adding images with PnP/Powershell

Oh, hello! Welcome back to our guest blogger series. This is your chance to learn a little something about the men and women behind the screen and discover what drives them.

Today we welcome back our Senior Web Developer Corrie Haffly for another trip into the world of Powershell and PnP. When we last left off, we learned how to build a basic site provisioning package and deploy it. But where else can we go? Enjoy part 2 of our look into Powershell and PnP.


In this article, we’ll be extending the simple deployment package from Get Started with PnP/Powershell! by adding our own images.

As a reminder, here is what the file structure looks like in the deployment package:

Folder structure of simple deployment package

Adding your own images will involve two steps:

  1. Add the image files into the folder.
  2. Add the image references to the XML.

Let’s get started!

Add the image files into the folder.

In Windows Explorer, copy some of your own image files into the pageimages folder.

Adding images to folder

Add the image references to the XML.

Open simple.xml for editing.

<?xml version="1.0"?>
<pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2016/05/ProvisioningSchema"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://schemas.dev.office.com/PnP/2016/05/ProvisioningSchema https://raw.githubusercontent.com/OfficeDev/PnP-Provisioning-Schema/master/OfficeDevPnP.ProvisioningSchema/ProvisioningSchema-2016-05.xsd">
    <pnp:Preferences Generator="OfficeDevPnP.Core, Version=1.6.915.0, Culture=neutral, PublicKeyToken=null" />
    <pnp:Preferences Generator="OfficeDevPnP.Core, Version=1.6.915.0, Culture=neutral, PublicKeyToken=null" />
    <pnp:Templates ID="CONTAINER-TEMPLATE-3231B31CFF0449948888AFFF43F57101">
        <pnp:ProvisioningTemplate ID="TEMPLATE-3231B31CFF0449948888AFFF43F57101" Version="1">      
            <pnp:Files>
                <pnp:File Folder="PublishingImages" Overwrite="true" Src="pageimages\sample-photo.jpg"></pnp:File>
            </pnp:Files>
        </pnp:ProvisioningTemplate>
    </pnp:Templates>
</pnp:Provisioning>

Look for the line of code that contains the image reference.

<pnp:File Folder="PublishingImages" Overwrite="true" Src="pageimages\sample-photo.jpg"></pnp:File>

Copy and paste.

<?xml version="1.0"?>
<pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2016/05/ProvisioningSchema"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://schemas.dev.office.com/PnP/2016/05/ProvisioningSchema https://raw.githubusercontent.com/OfficeDev/PnP-Provisioning-Schema/master/OfficeDevPnP.ProvisioningSchema/ProvisioningSchema-2016-05.xsd">
    <pnp:Preferences Generator="OfficeDevPnP.Core, Version=1.6.915.0, Culture=neutral, PublicKeyToken=null" />
    <pnp:Preferences Generator="OfficeDevPnP.Core, Version=1.6.915.0, Culture=neutral, PublicKeyToken=null" />
    <pnp:Templates ID="CONTAINER-TEMPLATE-3231B31CFF0449948888AFFF43F57101">
        <pnp:ProvisioningTemplate ID="TEMPLATE-3231B31CFF0449948888AFFF43F57101" Version="1">      
            <pnp:Files>
                <pnp:File Folder="PublishingImages" Overwrite="true" Src="pageimages\sample-photo.jpg"></pnp:File>
                <pnp:File Folder="PublishingImages" Overwrite="true" Src="pageimages\sample-photo.jpg"></pnp:File>
            </pnp:Files>
        </pnp:ProvisioningTemplate>
    </pnp:Templates>
</pnp:Provisioning>

Change the name of the image file to match one of your new image files. Repeat this for each new image that you want to add.

<?xml version="1.0"?>
<pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2016/05/ProvisioningSchema"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://schemas.dev.office.com/PnP/2016/05/ProvisioningSchema https://raw.githubusercontent.com/OfficeDev/PnP-Provisioning-Schema/master/OfficeDevPnP.ProvisioningSchema/ProvisioningSchema-2016-05.xsd">
    <pnp:Preferences Generator="OfficeDevPnP.Core, Version=1.6.915.0, Culture=neutral, PublicKeyToken=null" />
    <pnp:Preferences Generator="OfficeDevPnP.Core, Version=1.6.915.0, Culture=neutral, PublicKeyToken=null" />
    <pnp:Templates ID="CONTAINER-TEMPLATE-3231B31CFF0449948888AFFF43F57101">
        <pnp:ProvisioningTemplate ID="TEMPLATE-3231B31CFF0449948888AFFF43F57101" Version="1">      
            <pnp:Files>
                <pnp:File Folder="PublishingImages" Overwrite="true" Src="pageimages\sample-photo.jpg"></pnp:File>
                <pnp:File Folder="PublishingImages" Overwrite="true" Src="pageimages\computer.jpg"></pnp:File>
                <pnp:File Folder="PublishingImages" Overwrite="true" Src="pageimages\woman-on-computer.jpg"></pnp:File>

            </pnp:Files>
        </pnp:ProvisioningTemplate>
    </pnp:Templates>
</pnp:Provisioning>

This current schema will upload the overwrite the files and check them in as a draft. If you would like to publish the files, add the Level="Published attribute to each File element, like this:

<pnp:File Folder="PublishingImages" Overwrite="true" Src="pageimages\woman-on-computer.jpg" Level="Published"></pnp:File>

Save your file.

Provision the site and see your changes.

If you are continuing on from the previous tutorial, you can skip to step 4.

  1. Open Windows Powershell as an admin (right-click on Powershell and choose Run as administrator).
  2. CD to the folder that contains simple.ps1:
    cd: "C:\yourdirectory"
  3. Add in your credentials.
    1. $creds = Get-Credential
    2. Enter your credentials.
  4. Run the simple.ps1 file (with the URL to your own site, of course):
    .\simple.ps1 -TargetWebUrl "https://yoursite.com/sites/sitecollection" -Credentials $creds
  5. Wait. You will see a bar across the top which shows the progress of the deployment. This may take a few minutes, so grab a snack.

    Powershell screenshot

  6. When it is complete, go to your site in the browser, go to the Images folder, and you’ll see that the images have been added to your site!

    New images added

Additional notes

  • You can re-provision your site as many times as you want. Adding files with PnP will rewrite the existing version of the file that is already up, so if, for example, you want to replace the computer.jpg image with an updated version, you can replace the computer.jpg image in your folder structure with a new version, and then redeploy the script. PnP/Powershell will push the new version of the image up to the site.
  • PnP/Powershell provides a way to easily reproduce site structures across site collections. You can easily deploy this to a different site collection, as many times as you want, and it will do the same thing every time. As you think about how PnP/Powershell can deploy content types, list structure, page layouts, and more, you can appreciate even more the powerful site templates that you can create with a PnP/Powershell solution.

Corrie Haffly Corrie Haffly
Senior Web Developer
Sea Otter Enthusiast
Your SharePoint Tour Guide

Leave a Reply

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

Human Check *

Filed Under

Our November Webinar is next week, and we hope you can join us! https://t.co/PS7fEexhKe https://t.co/Z2I8jc5udD

@EricOverfield at SPS Vienna #spsvienna https://t.co/PLzIXmRflV

Our friends from @pixelmillteam at #spsvienna https://t.co/FQgEO1s2GO

Missed our webinar ft. @buckleyplanet talking about @MicrosoftTeams? We've got the recap ready & waiting for you!… https://t.co/FwOpQ3CvVS

Missed our webinar ft. @buckleyplanet talking about @MicrosoftTeams? We've got the recap ready & waiting for you!… https://t.co/CDqD2ZHnB9