Getting document by file name with PowerShell using CAML

Getting one document from a document library by specifying the document’s name is possible by using CAML. With this method the looping through all the documents is avoided.

Here is a sample script to get the document with name ‘Doc1.docx’ from a document library:

$web = Get-SPWeb -Identity http://weburl
$list = $web.Lists["ListName"];
$query = New-Object Microsoft.SharePoint.SPQuery
$caml ='<Where><Eq><FieldRef Name="FileLeafRef"/><Value Type="File">Doc1.docx</Value></Eq></Where>'
$query.Query = $caml
$query.RowLimit = 1
$items = $list.GetItems($query)
$item = $items[0]

Advertisements

One Response to Getting document by file name with PowerShell using CAML

  1. Benjamin says:

    Thanks for this. My head stops hurting now from banging it on the keyboard.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: