Home > PowerShell, SharePoint 2010 > How to write PowerShell code part 4 (using loop)

How to write PowerShell code part 4 (using loop)

In this post, I’d like to show you how to loop through the xml element. I will use the list data deletion script as an example. You can download the script here.

1. To perform the loop, I use foreach in powershell. Here is my xml looks like


<?xml version="1.0" encoding="utf-8"?>
<Site Url="http://workflowuat/npdmoc">
 <Lists>
 <List Name="YBBEST Collaboration Areas" Type="Document Library"/>
 <List Name="YBBEST Project" />
 <List Name="YBBEST Document"/>
 </Lists>
</Site>

2. Here is the PowerShell to manipulate the xml. Note, you need to get to the $configurationXml.Site.Lists.List variable rather than $configurationXml.Site.Lists


foreach ($list in $configurationXml.Site.Lists.List){
 AppendLog "Clearing data for $($list.Name) at site $weburl" Yellow
 if($list.Type -eq "Document Library"){
 deleteItemsFromDocumentLibrary -Url $weburl -ListName $list.Name
 }else{
 deleteItemsFromList -Url $weburl -ListName $list.Name
 }
 AppendLog "Data in $($list.Name) at $weburl is cleared" Green
}

How to write PowerShell code part 1
How to write PowerShell code part 2
How to write PowerShell code part 3
How to write PowerShell code part 4

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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: