Just recently I've been having a heap of fun writing Powershell to aid a SharePoint content migration. Looking at one of the scripts I figured it's a nicely condensed way to demonstrate a few useful Powershell goodies whilst serving a useful purpose in SharePoint.

The full script is below, but here's the items worth noting;

$ErrorActionPreference = "Stop"

If you're nervous about your script running out of control, add the above line at the top, and it will stop on the first error it encounters.

$docSetInfos = Import-CSV C:\datafiles\DocSetData.csv -Header InvoiceID,Department,InvoiceDate,GrossAmount

Not only can I import data from a CSV really easily, but I tell Powershell what headers to apply so I can use dot notation later on to retrieve the specific values from each row.


I add properties to the document set by adding values to the hash table.

As far as the csv file, it doesn't need a header row, for my example it might look like this;


The above would create three document sets called X45363, Y76767 and Z76360.

Full script below. I suggest you paste it into an editor like Notepad++ to get a proper look at it. (and Happy SharePointing!);



$ErrorActionPreference = "Stop"
$ver = $host | select version 
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"} 
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) 
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell" 
Add-PSSnapin Microsoft.SharePoint.PowerShell 

$DestinationWebURL = "http://myPortal/sites/mySiteCollection"
$DestinationLibraryTitle = "MyLibrary"
$docSetInfos = Import-CSV C:\datafiles\DocSetData.csv -Header InvoiceID,Department,InvoiceDate,GrossAmount

$dWeb = Get-SPWeb $DestinationWebURL
$dList = $dWeb.Lists | ? {$_.title -like $DestinationLibraryTitle}

$cType = $dList.ContentTypes["My Custom Document Set Content Type"]

foreach($docSetInfo in $docSetInfos)
#Build properties hash table from $docSetInfos[]
[Hashtable]$docsetProperties = @{}

$InvoiceDateTime = Get-Date $docSetInfo.InvoiceDate
$NewFolder = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($dlist.RootFolder,$docSetInfo.InvoiceID,$cType.Id, $docsetProperties)