sure if you have ever had to need to grab the metadata column values from a huge library in a SharePoint site.
Today I needed to grab specific column values from a library with over 50k rows in it. The great thing is with PowerShell you can absolutely do it. Getting the data was easy, but it took me a bit to toss the data into an array so that I could export it cleanly into a .csv file. Â Originally I was just dumping it with Write-Out then piping that to Out-File command into a text file, but with Out-file it was putting each column’s value into a new line. (Apparently that’s default with Write-Out in PowerShell). So I had to bite the bullet and use the Export-CSV command. Of course I needed to move my For-Each into an array and that’s when things got fun for a while.
Anyways, got it working. As with all my scripts if you see a better way to make this go. Please leave me a comment!
Hopefully this helps and saves someone some time in the future.
[code language=”powershell”]
if((Get-PSSnapin | Where {$_.Name -eq “Microsoft.SharePoint.PowerShell”}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}$Web = “https://spsiteurl”
$ListName = “Shared Documents”$ExportFolder = “C:\ExportPath\”
$ExportName = Get-Date -f “dd-MM-yyyy-hh-mm-ss”
$ExportPath = $ExportFolder + $ListName + $ExportName + “.csv”$sWeb = Get-SPWeb $Web
$sList = $sWeb.Lists[$ListName]
$sItems = $sList.GetItems()$details = @() #I’m 80% sure you don’t need to declare this array
$results = @() #You MUST declare this array. (arrays love arrays homie)# These will be the names of the columns you want to get from the list.
$sItems | ForEach-Object {
$_[‘ID’], $_[‘Title’], $_[‘Modified By’], $_[‘Created’], $_[‘Project’], $_[‘Status’]# I created alias variables below because I ran
# into some items that didn’t convert into or from strings. You may not need this.
$DocID = $_[‘ID’]
$DocTitle = $_[‘Title’]
$DocModifiedBy = $_[‘Modified By’]
$DocCreatedBy = $_[‘Created’]
$DocProject = $_[‘Project’]
$DocStatus = $_[‘Status’]#Build an array called details
$details = @{
DocumentID = $DocID
DocumentTitle = $DocTitle
ModifiedBy = $DocModifiedBy
CreatedBy = $DocCreatedBy
DocumentProject = $DocProject
DocumentStatus = $DocStatus
}$results += New-Object PSObject -Property $details
}$results | Export-Csv $ExportPath -NoTypeInformation
$sWeb.Dispose()[/code]
Happy SharePointing.
(590)
I think there is an error on this line:
$sItems | ForEach-Object {
Shouldn’t it be:
$spSourceItems | ForEach-Object {
Thanks Steven! I changed the $spSourceItems = $sList.GetItems() to $sItems = $sList.GetItems()
I was cleaning up the script variable names to make it more generic and missed that line!
Since I do have you though, do I need that Array declaration for $details = @() ?
I think it’s redundant and not used, but arrays are still tricky to me so not sure. 🙂