Skip to content

Instantly share code, notes, and snippets.

@fluxdigital
Last active October 30, 2018 18:23
Show Gist options
  • Save fluxdigital/7d87553918faf5a1a5cb21879dd3d513 to your computer and use it in GitHub Desktop.
Save fluxdigital/7d87553918faf5a1a5cb21879dd3d513 to your computer and use it in GitHub Desktop.
SPE function to update the data template of an item (and optionally it's child items) from one template to another
<#
.SYNOPSIS
Updates the data template of an item (and optionally it's child items) from one template to another
.PARAMETER RootItemId
Specifies an Item Id to start searching for items from
.PARAMETER SourceTemplateId
Specifies the Id of the template to match items on (the template you wish to change)
.PARAMETER TargetTemplateId
Specifies the Id of the template to change items to
.PARAMETER Recurse
Specifies if all the child items below RootItemId should be included in the search.
.EXAMPLE
Change-Template -RootItemId 36584658-7B62-4572-932E-8214A3CD7CE8 -SourceTemplateId 92C1C171-3ED0-4E8F-860A-FEAF14336A96 -TargetTemplateId DD68EBBB-4D82-411C-BA19-D95A65AAEF83 -Recurse $true
Updates the data templates of all items and childitems from one template to another
#>
function Change-Template{
[CmdletBinding()]
param(
[ValidateNotNullOrEmpty()]
[string]$RootItemId = "",
[ValidateNotNullOrEmpty()]
[string]$SourceTemplateId ="",
[ValidateNotNullOrEmpty()]
[string]$TargetTemplateId = "",
[bool]$Recurse = $false
)
$rootItem = Get-Item -Path master: -ID $RootItemId;
$sourceTemplate = Get-Item -Path master: -ID $SourceTemplateId;
$targetTemplate = Get-Item -Path master: -ID $TargetTemplateId;
Write-Host "Updating Templates for child items of '$($rootItem.Name)' from '$($sourceTemplate.Name)' to '$($targetTemplate.Name)'"
$itemCount = 0;
Get-ChildItem $rootItem.FullPath -Recurse:$Recurse | Where-Object { $_.TemplateName -eq $sourceTemplate.Name} | ForEach-Object {
Set-ItemTemplate -Item $_ -TemplateItem $targetTemplate
$itemCount +=1;
}
Write-Host "Completed Updating '$($itemCount)' Items"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment