I am trying to setup a script to copy documents from a document library to a local file share. I have found a handful of ways to do this, but I have one caveat. I need script to look at a value in a field and only copy those that match a certain value. My field is named category. How / where would I add something like "if category -eq red" to the script below to only copy files where category was "red". I am still learning Powershell and only understand the very basics at this point.

Here is a script that I found / modified from Download and delete documents using Powershell

# Add SharePoint Snapin to PowerShell            
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)     {            
  Add-PSSnapin Microsoft.SharePoint.PowerShell            

$destination = "C:\\test\\"
$web = Get-SPWeb -Identity "http://TestSite/"
$list = $web.GetList("http://TestSite/testlib/")
$items = $list.Items;

function ProcessFolder {
$folder = $web.GetFolder($folderUrl)
foreach ($file in $folder.Files) {
    #Ensure destination directory
    $destinationfolder = $destination + "/" + $folder.Url 
    if (!(Test-Path -path $destinationfolder))
        $dest = New-Item $destinationfolder -type directory 
    #Download file
    $binary = $file.OpenBinary() 
    $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
    $writer = New-Object System.IO.BinaryWriter($stream)


#Download root files
#Download files in folders
foreach ($folder in $list.Folders) {

I am a bit confused as to how to get access to field values as well as whether I'm supposed to pipe a where or use an if. Any help would be greatly appreciated!

Thank you very much.

foreach ($file in $folder.Files | ?{ $_.Item["category"] -eq 'red'  }) {

This will filter the files for ones that's underlying List Item has a category value 'red'.

  • Worked wonderfully!
    – Chris
    May 1 '14 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.