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

Upload files to SharePoint Library with Metadata

Structure the 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 the previous article, we created a SharePoint document library with four columns for metadata. This metadata will be 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. This is done in PowerShell with the split method.

In the demo below, Peter Kalmström creates a PowerShell class with four properties. These properties correspond to the metadata we want to add to SharePoint. Then he creates an instance of that class for each file and assigns the properties from the file to it.  

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)
}

In the next article, we will expand the forEach loop so that it distributes the metadata into the correct SharePoint library columns when files are uploaded.

Do you want more detailed explanations? Wait for the book, it will soon be published!


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

Security and integrity

Copyright  Kalmstrom Enterprises AB  All rights reserved