#script reads from xml file to add crawled properties and managed properties
cls
#add sharepoint cmdlets
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin Microsoft.SharePoint.PowerShell
}
#get the XML file
[System.Xml.XmlDocument] $XmlDoc = new-object System.Xml.XmlDocument
$file = resolve-path(".\searchprop.xml")
if (!$file)
{
Write-Host "Could not find the configuration file specified. Aborting." -ForegroundColor red
Break
}
write-host "Parsing file: " $file
$XmlDoc = [xml](Get-Content $file)
#get the node containing the name of the search service application where you want to add properties
$sa = $XmlDoc.SearchProperties.ServiceName
$searchapp = Get-SPEnterpriseSearchServiceApplication $sa
#loop through crawled properties to check or add -- don't add if it already exists
$CrawledPropNodeList = $XmlDoc.SearchProperties.CrawledProperties
foreach ($CrawledPropNode in $CrawledPropNodeList.CrawledProperty)
{
$SPCrawlProp = $CrawledPropNode.Name
$SPCrawlPropType = $CrawledPropNode.Type
#Create Crawled Property if it doesn't exist
if (!(Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Name $SPCrawlProp -ea "silentlycontinue"))
{
switch ($SPCrawlPropType)
{
"Text" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 31 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
"Integer" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 20 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
"Decimal" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 5 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
"DateTime" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 64 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
"YesNo" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 11 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
default {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 31 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
}
}
}
#now that the crawled properties exist, loop through managed properties and add
$PropertyNodeList = $XmlDoc.SearchProperties.ManagedProperties
foreach ($PropertyNode in $PropertyNodeList.ManagedProperty)
{
$SharePointProp = $PropertyNode.Name
$SharePointPropType = $PropertyNode.Type
$SharePointPropMapList = $PropertyNode.Map
#add managed property
#remove it if it already exists
if ($mp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $SharePointProp -ea "silentlycontinue")
{
#$mp | Remove-SPEnterpriseSearchMetadataManagedProperty -Confirm
$mp.DeleteAllMappings()
$mp.Delete()
$searchapp.Update()
}
}To Delete Managed Properties in SharePoint 2013 Using PowerShell.
