Came to the below. It exports the webparts locally, deletes them from the page, uploads them and inserts them into the Wiki Content on the page:
$Web.AllowUnsafeUpdates = $true;
$Page.CheckOut()
#Start moving web parts...
Write-host "========== Starting the web parts on" $Page.url " ======-ForegroundColor Green $errorMsg = "Test Error Message" $exportFolderPath = "D:\SharePoint\webpartsTemp" $wpm = $web.GetLimitedWebPartManager($Page.ListItem.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) foreach($wp in $wpm.WebParts) { $wp.ExportMode="All"; $webpartfile = $wp.Title + ".xml" Write-host "=== Exporting " $webpartfile -ForegroundColor Yellow $exportPath = $exportFolderPath + "\"" + $webpartfile $xwTmp = new-object System.Xml.XmlTextWriter($exportPath,$null); $xwTmp.Formatting = 1;#Indent $wpm.ExportWebPart($wp, $xwTmp); $wpm.DeleteWebPart($wpm.WebParts[$wp.ID]) $xwTmp.Flush(); $xwTmp.Close(); Write-host "Done exporting " $webpartfile Write-host "Loading xml..." $wpxml = Get-Content -Path $exportPath # Initialize the XmlReaderSettings Object which is required for the XmlReader Object Write-host "Initialize the XmlReaderSettings Object which is required for the XmlReader Object" $xmlReaderSettings = New-Object System.Xml.XmlReaderSettings # Create the XmlReader Object by using the WebPart Definition file and the ReaderSettings Object Write-host "Create the XmlReader Object by using the WebPart Definition file and the ReaderSettings Object" $xmlReader = [System.Xml.XmlReader]::Create($exportPath,$xmlReaderSettings); #Add Web Part to catalogs folder and Get the WebPart Definition Object based on the webpart definition xml file Write-host "Add Web Part to catalogs folder and Get the WebPart Definition Object based on the webpart definition xml file " -ForegroundColor Yellow $oWebPartDefinition = $wpm.ImportWebPart($xmlReader,[ref]$errorMsg); ## Create the web part. # Create a guid and storage key Write-host "Create a guid and storage key" $documentsWebPartGuid = [System.Guid]::NewGuid() $documentsWebPartId = "g_" + $documentsWebPartGuid.ToString().Replace("-","_") # Create a new web part Write-host "Create a new web part" $documentsWebPart = $oWebPartDefinition # Give the web part an Id Write-host "Give the web part an Id" $documentsWebPart.ID = $documentsWebPartId # Tell it to use the default view Write-host "Tell it to use the default view" $documentsWebPart.Title = $wp.Title ## Create the Wiki Content Write-host "Create the Wiki Content" $Page.ListItem['Page Content'] = $Page.ListItem['Page Content'] + @" <p></p> <div class='ms-rtestate-read ms-rte-wpbox' contenteditable='false' defaultpastemoderesolver='PastePlain'> <div class='ms-rtestate-notify ms-rtestate-read $($documentsWebPartGuid.ToString('D'))' id='div_$($documentsWebPartGuid.ToString('D'))'> </div> <div id='vid_$($documentsWebPartGuid.ToString('D'))' style='display: none;'> </div> </div> <p> </p> "@ $Page.ListItem.Update() # Add the web parts to the page Write-host "Add the web parts to the page" $wpm.AddWebPart($documentsWebPart,'wpz',0) $Page.ListItem.Update(); # Remove expired xml Write-host "Remove expired xml" Remove-Item $exportPath } #Done with web parts...
May have to add the webparts to an array, and then work through that array (to prevent the script failing as soon as the first web part is removed), but this works.