Copy sharepoint list item using powershell script

Few days ago i wrote about the problem that How an anonymous user could start a workflow in sharepoint. The solution was to create and schedule a powershell script to update the list items in a given Sharepoint list using a real named account. So the workflow can be started in the name of this user. The WorkFlow is triggered by a modification.

But in that case we can have a problem if we don’t check wether if the item was already updated or not. And what if we don’t want anonymous users to have a look at each others items in the given list? (okay, okay i know let’s do an empty view and such workaround solution…) So a simple solution could be to copy these list items to another list.

As the copy is made by using a named account you can have a WorkFlow triggered by new item creation at the destionation list. So every workflow runs only once. You do not need to check anything during the workflow…

So here you are a sample code to copy list item from a given list to another one:

$site = new-object Microsoft.SharePoint.SPSite("http://moss")
$web = $site.AllWebs | where { $ -eq "TestSite"}
$lista =$web.Lists["CopyFromList"]
$listb =$web.Lists["CopyToList"]
$items = $lista.items
foreach ($item in $items) {
write-host -foregroundcolor yellow $item["Title"]
$newitem= $listb.items.Add()
$newitem["Title"] = $item["Title"]
$newitem["AnotherField"] = $item["AnotherField"]
start-sleep 1
start-sleep 10

Of course both lists have to have the same structure or at least the CopyToList has to have the fields of the CopyFromList.
This script also has to be scheduled at the Sharepoint server locally.

You can schedule this script as well. Have fun!


8 thoughts on “Copy sharepoint list item using powershell script”

  1. Hi I am using the script though it give out put but it throws error named “An error occurred while enumerating through a collection: Collection was modified”.I have tried a lot to resolve but no hope.if the list contains more thatn one item should we need to execuste each time.please help

    1. Hey Anurag,

      Tahat seems to me that you are manipulating the source list while you are copying the items (eg dele item after you already copied that one). Usually this error message happens then 🙂
      So in that case you have two solutions:
      1. after an item copied you get the source list once more into your variable or
      2. start the copy from the end of the source list
      If you delet an item in the begining of the source list all the remaining item’s actual ID in the list will change and jump one up. If you start to copy and delet from the end of the source list there won’t be effect on the deletition 🙂

      was it clear? 🙂 seeyou

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s