Home >Tips >PowerShell with SharePoint from Scratch >Upload files to SharePoint with metadata - upload kalmstrom.com site map icon Site map  

Upload files to SharePoint Library with Metadata

Upload Files and Metadata

A PowerShell with SharePoint tutorial

PowerShell iconSharePoint has several methods to upload files from Windows Explorer to a document library, but if you want to do it automatically, you should use a PowerShell script. In earlier articles, we have seen how do create such a script and how set a task to run it automatically.

In two earlier articles we have prepared the addition of not just the files but also four categories of metadata to the SharePoint library. This metadata is taken from files in a Windows Explorer folder named CVs.

The folder has three subfolders: Hired, Maybe and No. A file's placement in one of those folders gives us the first keyword.

The files are all named in a consistent way, so the file names can be used to extract three more keywords for each file: first name, last name and department.

In the demo below, Peter Kalmström uses PowerShell key-value pairs to direct each keyword to the correct SharePoint library column. 

Peter created some parts of the script below in the previous demo. The demo below is the last one in this short series about metadata retention, and it show how Peter ties everything together and get the files with their metadata added to SharePoint. 

Class CV{
    $FirstName
    $LastName
    $Department
    $Decision
}

$AllCVFiles = Get-ChildItem -Path "C:\Users\PeterKalmström\Documents\CVs" -Recurse -File 

foreach($CVFile in $AllCVFiles){
    #$CVFile = $AllCVFiles[0]
    $CVObj = New-Object CV
    $CVObj.Decision = $CVFile.DirectoryName

    $FileNameParts = $CVFile.BaseName -split ' - '
    $CVObj.Department = $FileNameParts[1]

    $NameParts = $FileNameParts[0] -split ', '
    $CVObj.LastName = $NameParts[0]
    $CVObj.FirstName = $NameParts[1]
    #Write-Host (ConvertTo-Csv $CVObj)

    $CVValues = @{}
    $CVValues += @{'Title'=$CVFile.BaseName }
    $CVValues += @{'CVDecision'=$CVObj.Decision}
    $CVValues += @{'CVFirstName'=$CVObj.FirstName }
    $CVValues += @{'CVLastName'=$CVObj.LastName}
    $CVValues += @{'CVDepartment'=$CVObj.Department}
    Add-PnPFile -Path $CVFile.FullName -Folder "CVs" -Values $CVValues
}

Create a task in Windows Task Scheduler to automate the running of this script.


back icon next icon
Products Buy FAQ Services Tips Books Contact About Us Tools

Security and integrity

Copyright  Kalmstrom Enterprises AB  All rights reserved