Updating SP2013 refiners using PowerShell

In SharePoint 2010, changes to the refiners were made using the the FilterCategoriesDefinition property of the refiners web part which held an XML blob.  In SharePoint 2013 we instead do this using the SelectedRefinementControlsJson property.  As the name implies, the configuration is now in JSON format.  Conveniently PowerShell 3.0 now includes the ConvertTo- and ConvertFrom-Json cmdlets that make this a breeze.  Here’s a code snippet that changes the display name for the Last Modified Date refiner to “Mod Time” (all relevant error handling removed for clarity):

 # Get our page and check it out
$spweb = Get-SPWeb "https://da-server/search/"
$page = $spweb.GetFile("Pages/results.aspx")
$page.CheckOut()

# Find the Refinement web part
$webPartManager = $spweb.GetLimitedWebPartManager("https://da-server/search/Pages/results.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$rwp = $webpartmanager.webparts | ? { $_.title -eq 'Refinement' }

# Refiners are updated by changing the JSON
$j = $rwp.SelectedRefinementControlsJson | ConvertFrom-Json
$j.refinerConfigurations | % { if ($_.propertyName -eq 'LastModifiedTime') { $_.displayName = 'Mod Time'; }
$rwp.SelectedRefinementControlsJson = ConvertTo-Json $j -Compress

# Save our changes
$webpartmanager.SaveChanges($rwp)          # save changes to webpart
$page.CheckIn('Changed last mod refiner')
$page.Publish('Changed last mod refiner')

Now, after reloading my search results page, I see the new display name:

modtime