Tuesday, August 7, 2012



Environment: SharePoint 2010 / Powershell

Issue:

I have been asked by a colleague about an issue in SharePoint 2010 Management Shell command prompt. The issue was how to use loop(for e.g. For each loop) in this command prompt to print names of a document list library within a SharePoint site. 


Solution:

Here is an example. The trick is that once you complete your statement and close your parenthesis then press TWO enters to get your output (see below). Users only click one time to get result and think it is not working. 



#================================================================================
# The following cmdlets use Foreach loop on Powershell cmd prompt with a condtion to show only PDF file #
#===============================================================================#

PS C:\Users\epmadminproc_d> $SPWeb = Get-SPWeb -identity  http://site/pws/projectsite1
PS C:\Users\epmadminproc_d> $SDlist = $SPWeb.Lists["Site Documents"]
PS C:\Users\epmadminproc_d> $view = $SDlist.Views["All Documents"]
PS C:\Users\epmadminproc_d> $Items = $SDlist.GetItems($view)
PS C:\Users\epmadminproc_d> foreach ($Item in $Items){
>> if ($Item.name.Contains("pdf")) { write-host $Item.name}}
>>
Bobcat CW5 Milestone Schedule_V1.1.pdf
Bobcat Project Summary Schedule.pdf
PS C:\Users\epmadminproc_d>

No comments:

Post a Comment