SPE script: Copy Items

 Copy items

$newCategories = Get-ChildItem "/sitecore/Site1/Home" | Where-Object -Property TemplateName -eq "Category"

$source = '/sitecore/content/Site1/Articles'
$target = '/sitecore/content/Site2/Articles'

$targetItem = Get-Item $target
$allItems = Get-ChildItem $source

$healthTopicImageFolder = Get-Item "/sitecore/media library/Site1/Articles"

$allItems | ForEach-Object {
$newItem = $_.CopyTo($targetItem, $_.Name)
$newItem.Editing.BeginEdit();

# Reset Page design
$newItem["Page Design"] = "";

# Replace category if possible
$categoryItem = Get-Item $newItem["Category"]
IF ($categoryItem) {
  $newCategoryItem = $newCategories | Where-Object -Property Name -eq $categoryItem.Name
  IF ($newCategoryItem) {
$newItem["Category"] = $newCategoryItem.ID;
}
}

# Hero Image
$oldImage = [Sitecore.Data.Fields.ImageField]$newItem.Fields["Image"];
$newImage
if ($oldImage) {
$newImage = $oldImage.MediaItem.CopyTo($healthTopicImageFolder, $oldImage.MediaItem.Name);
$oldImage.MediaID = $newImage.ID;
}

# Thumbnail
$oldThumbnailImage = [Sitecore.Data.Fields.ImageField]$newItem.Fields["ListingThumbnail"];
if ($oldThumbnailImage) {
if ($oldThumbnailImage.ID -eq $oldImage.ID) {
$oldThumbnailImage.MediaID = $newImage.ID;
}
else {
$newThumbnail = $oldThumbnailImage.MediaItem.CopyTo($healthTopicImageFolder, $oldThumbnailImage.MediaItem.Name);
$oldThumbnailImage.MediaID = $newThumbnail.ID;
}
}

$newItem.Editing.AcceptChanges();
}