Sometimes you may need to measure the user adoption after a new SharePoint deployment or some enhancements done in a existing farm. SharePoint provides some useful metrics, but a good way to measure this would be to get a simple count of items that match a specific criteria. For example, how many new documents have been uploaded by users in a Site Collection. Like many other questions in SharePoint 2010, PowerShell is the perfect tool to answer them.

We can always create a script that traverse the Sites, Document Libraries and get the count or other information that we need to know about the items, but this method can be slow and is not scalable.

The following example will get the count of all files contained in all Document Libraries in a Site Collection or several Site Collections using the Search engine and PowerShell.

If you have been using PowerShell, you already know the scripting tools are very rudimentary, so I would recommend using PowerGUI, a free tool that adds some functionality and makes it easier to create and run scripts.

The script will accept 1 parameter, which is an array of the Site Collection URLs that we want to query.

 <span class="kw3">Param</span><span class="br0">(</span><span class="re0">$siteCollections</span><span class="br0">)</span>

After this, the script itself is very straightforward and basically we only have to construct the CAML query and call GetSiteData() method from SPWeb. Also, we need to set the MaxListLimit to 0 to indicate no limit on the query and set the QueryThrottleMode to Override to make sure we are not hitting the user list throttle.

From there, we only need to go through the array and query each Site Collection with the information that we need. In this simple case, we want to query only Document Libraries and get the URL of the file.

<span class="kw1">Add-PSSnapin</span> Microsoft.SharePoint.PowerShell <span class="sy0">-</span>ea SilentlyContinue
 
<a><span class="kw6">$ErrorActionPreference</span></a> <span class="sy0">=</span> <span class="st0">"stop"</span>
 
Try 
 
<span class="br0">{</span>
 
 <span class="kw3">foreach</span><span class="br0">(</span><span class="re0">$siteCollection</span> <span class="kw3">in</span> <span class="re0">$siteCollections</span><span class="br0">)</span>
 
 <span class="br0">{</span>
 
 <span class="re0">$site</span> <span class="sy0">=</span> Get<span class="sy0">-</span>SPSite <span class="sy0">-</span>Identity <span class="re0">$siteCollection</span> 
 
 <span class="re0">$q</span> <span class="sy0">=</span> <span class="kw1">New-Object</span> <span class="kw5">-TypeName</span> microsoft.SharePoint.SPSiteDataQuery
 
 <span class="re0">$q</span>.Lists <span class="sy0">=</span> <span class="st0">""</span>
 
 <span class="re0">$q</span>.ViewFields <span class="sy0">=</span> <span class="st0">""</span>
 
 <span class="re0">$q</span>.Query <span class="sy0">=</span> <span class="st0">""</span>;
 
 <span class="re0">$q</span>.QueryThrottleMode <span class="sy0">=</span> <span class="st0">"Override"</span>
 
 <span class="re0">$q</span>.Webs <span class="sy0">=</span> <span class="st0">""</span>
 
<span class="kw1">Write-Host</span> <span class="re0">$siteCollection</span> : <span class="re0">$site</span>.RootWeb.GetSiteData<span class="br0">(</span><span class="re0">$q</span><span class="br0">)</span>.Rows.Count
 
<span class="re0">$site</span>.Dispose<span class="br0">(</span><span class="br0">)</span>
 
 <span class="br0">}</span>
 
<span class="br0">}</span>
 
Catch
 
<span class="br0">{</span>
 
 <span class="kw1">Write-Host</span> <span class="kw5">-ForegroundColor</span> red <a><span class="kw6">$_</span></a> <span class="sy0">|</span> <span class="kw2">fl</span> <span class="sy0">*</span> <span class="kw5">-Force</span>
 
<span class="br0">}</span>

GetSiteData() method returns a dataTable with all items that match the criteria. As you can see the query is extremely simple, but we could do other more complex queries like getting just specific document types, items from a specific Content Type, etc.

To execute the script, we only run it from the PowerGUI console or Powershell console passing the parameters in an array.

 .\DocumentCount.ps1 <span class="sy0">@</span><span class="br0">(</span>http:<span class="sy0">//</span>shrepoint<span class="sy0">/,</span>"http:<span class="sy0">//</span>sharepoint<span class="sy0">/</span>sites<span class="sy0">/</span>site1"<span class="br0">)</span>

The output will look like this:

http://sharepoint: 32145

http://sharepoint/sites/site1: 2014