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>

Environment: SharePoint 2010 



Powershell cmdlets have been become so popular for Administrators and Developers in the SharePoint 2010 world to automate your process and update the existing current SharePoint environments. You can use the same SharePoint object model and can do a lot of work using Powershell scripts without using Visual Studio. In this post, i am trying to give some most common examples/scenarios for those who are new in PowerShell to give an idea how cool and helpful this tool is in the SharePoint 2010.



Examples:

For Help:

Use Get-Help verb with the available cmdlets i.e.





For Site collections/Sites:










  For Lists or Document library:







If you have any comments or questions, please drop a line.

Happy Powershell scripting!