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. It‘s quite in here! Why not leave a response?




Subscribe without commenting