Here comes a quick PowerShell tip at the end of the week. Earlier I have posted a script (found at the download section) that helps copy SharePoint List Items from one list to another, read more about it here.

But today I wanted to use the script to copy items to a specific folder in that list and it was a bit tricky at the first to understand how this could be done. But after some testing I figured it out and this is how you do.

# First open the list to create the new item in.
$TargetWeb = New-Object Microsoft.SharePoint.SPSite(http://yoursite)
$OpenTargetWeb = $TargetWeb.OpenWeb()
$OpenTargetList = $OpenTargetWeb.Lists[$TargetList]

# Then we need to get the folder name and set the objecttype
$FSObjectytype = [microsoft.sharepoint.SPFilesystemobjecttype]::File
$Folder = $targetweb.GetFolder(
http://urlToFolder)

# Now we can create the new Item
$NewItem = $OpenTargetList.items.Add($Folder.Folder.ServerRelativeUrl, $FSObjectytype, $null)

# Add values
$NewItem["Title"] = “Yada Yada”

# Finally use update to create the new item
$NewItem.update()

My intention is to add this to my copy items script later on.



  1. Perla Lucero (Reply) on Friday 22, 2010

    Hi Matt i have a few problems with the URL of my folder, i don´t know where i can see it, can u help me? maybe is a seally question but i´m new in Powershell and Sharapoint ! :(

    hi from México.

    • Mattias Karlsson (Reply) on Friday 22, 2010

      Hi Perla,

      I’m a bit unsure about your question. When opening a document library and then browse down the structure you will se that in your url you get the name of the folder in the url. e.g. http://SPServer.com/DocLibrary/folder1/folder2

      Does that help?