I want to remove a Termset that contains several terms. How do I accomplish this?
1 Answer
You need to have the initial variables at hand as well as the Term Store Name (in the @store variable), to make this work.
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
# Initial Variables
$SiteCollectionUrl = "http://portal/"
$termGroupName = "Intranet"
$termSetNames = @("TermSet1", "TermSet2")
function RemoveTermSet([Microsoft.SharePoint.Taxonomy.TermStore]$store, [string]$groupName)
{
if ($group -ne $null)
{
$group.TermSets|foreach{
if($termSetNames -ccontains $_.Name)
{
Write-Host $_.Name "will be deleted"
$_.Delete()
$store.CommitAll()
}
}
}
}
$session = Get-SPTaxonomySession -Site $SiteCollectionUrl
$store = $session.TermStores["Managed Metadata Service"]
$group = $session.TermStores.Groups | Where-Object {$_.Name -eq $termGroupName}
RemoveTermSet @store $termGroupName
$store.CommitAll()
-
1Working script, but is it not a bit weird to be dependent on a variable declared in an outer scope in RemoveTermset function? :)Robert Lindgren– Robert Lindgren2013-11-07 17:56:31 +00:00Commented Nov 7, 2013 at 17:56
-
1@RobertLindgren True, but I needed a quick fix not to destroy the whole term group :)2013-11-07 18:36:01 +00:00Commented Nov 7, 2013 at 18:36