A PowerShell script that uploads document metadata in a CSV file to a SharePoint document library.
This script works for accounts that have 2FA enabled and requires fewer SharePoint permissions than other similar scripts I've come across.
First, you will need to ensure that you are using the old version of Windows PowerShell (5.1). You can check what version of PowerShell you're using with the command $PSVersionTable.PSVersion
.
Version 5.1 comes preinstalled with some versions of Windows. If you don't have it, you can install it from here.
To check if it's already installed:
Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable | Select-Object Name,Version
To install: Install-Module Microsoft.Online.SharePoint.PowerShell
For more detailed information, check the docs here.
To install: Install-Module SharePointPnPPowerShellOnline
NOTE: This will install the legacy version of the module. This is necessary for the script to work.
You will need to change a few lines in the script.
Change the below lines to match the directory where the following files were installed. It should be somewhere in the WindowsPowerShell\Modules\Microsoft.Online.SharePoint.PowerShell
folder, wherever your Windows PowerShell was installed. You might have to search to find them.
# Load SharePoint CSOM Assemblies
Add-Type -Path "C:\your-path-here\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\your-path-here\Microsoft.SharePoint.Client.Runtime.dll"
Similar to the previous step, change the following line to match the directory where the OfficeDevPnP.Core.dll
file was installed. It should be somewhere in the WindowsPowerShell\Modules\SharePointPnPPowerShellOnline
folder, wherever your Windows PowerShell was installed.
# Load PnP Core
Add-Type -Path "C:\your-path-here\SharePointPnPPowerShellOnline\3.29.2101.0\OfficeDevPnP.Core.dll"
Edit the following lines to match your files:
# Parameters
$SiteURL = "https://yoursite.sharepoint.com/sites/yoursite"
$LibraryName = "Library Name"
$LibraryRelativeURL = "/sites/yoursite/Library Name/"
$CSVFile = "C:\your-path-here\metadata_file.csv"
Make sure that the file name field matches the field name in your CSV file. The default is "File Name". This can be changed if necessary.
$SourceFilename = $Row."File Name"
Finally, you should update the metadata fields to match the ones in your CSV file and document library. The default ones ("DocTypeName", "DocDate", "FiscalYear"...) are the ones needed for my project; they can be changed to whatever you need them to be. The name in brackets ($ListItem["FiscalYear"]
) is the name of the field in your SharePoint document library. The name not in brackets ($Row."Fiscal Year"
) is the corresponding field name in your CSV file. They can be different.
# Update metadata fields
$ListItem = $File.ListItemAllFields
$ListItem["DocTypeName"] = $Row."DocTypeName"
$ListItem["DocDate"] = $Row."DocDate"
$ListItem["FiscalYear"] = $Row."Fiscal Year"
...
This script does not upload files; it only changes the metadata. I could not get uploading files to work without elevated permissions in SharePoint, which did not work for my case.
First, you will need to create a SharePoint document library with all of the correct metadata fields.
Next, you will need to upload all of the files, either manually or by using another script. I was able to drag-and-drop 1,000 files at a time from my computer to SharePoint, so it didn't take too long. If you would rather use a script, you can check the Resources section below for some help with that (assuming permissions are not an issue).
- Run Windows PowerShell. Make sure that you are using the old version (5.1).
- In the Windows PowerShell window, use
cd
to move to the folder containing the script. - Run the script using the command
.\SharePoint-Metadata-Uploader.ps1
.
I found that I had to put all of the SharePoint fields into the script as single words (no spaces). The actual fields on SharePoint can have spaces, but those spaces must be removed in the script (e.g. my "Fiscal Year" field in SharePoint was written in the script as "FiscalYear"). I'm not sure why, but if you're having issues, that might be something to try.
Here are some links that I found helpful while putting this together:
- SharePoint Diary - SharePoint Online: How to Update Document Properties using PowerShell?
- SharePoint Diary - SharePoint Online: Import Files from a CSV using PowerShell
- SharePoint Diary - Upload Files to SharePoint Library Remotely using Client-Side Object Model (CSOM) and PowerShell
- Stack Exchange - update document properties in library using csom