This script exports SharePoint 2010 keywords, synonyms, and best bets to csv files

-----------------------------------------------------------
 Export keywords from SP-2010 --> CSV files

 This script will export Sharepoint 2010 
 keywords/bestbets/synonyms in to CSV files.
-----------------------------------------------------------

 Definition:

 This script is used for exporting keywords/bestbets/synonyms into CSV files
 from a Sharepoint 2010.

 Example: .\export_SP2010_keywords_to_CSV.ps1 -c .\configuration.xml

configuration.xml:

<config>
 <export_SP2010_CSV>
  <!-- The Microsoft Search Server 2010 site to export keywords/bestbets/synonyms into CSV files -->
  <SPsiteURL>http://fs14/</SPsiteURL>  
  <!-- The path+filenames for the output CSV files -->
  <output_keywordsfile>.\csv\keyword.csv</output_keywordsfile>
  <output_bestbetsfile>.\csv\bestbet.csv</output_bestbetsfile>
  <output_synonymsfile>.\csv\synonym.csv</output_synonymsfile>
 </export_SP2010_CSV>

</config>

PowerShell
Edit|Remove
#  ESP - Export keyword tool from SP-2010 --> CSV files
#
#  This tool will export SP-2010 keywords|bestbets|synonyms into CSV files
#  which you can later use with "import_CSV_FS4SP.ps1" script to import into FS4SP
#  configuration. 
#  contributors: ceaykan, dabenson, brentgr 
#  version: 1.02
#

cls

$global:configFile = ""
$global:siteid=""
$global:keywordsfile=""
$global:bestbetsfile=""
$global:synonymsfile=""
$global:proceed = $False
$global:help = "`n`n Definition : `n`n This tool is used for exporting keywords/bestbets/synonyms "+
              "into CSV files `n from a Sharepoint 2010. The output of these files can be used as "+
              "input for `n import_CSV_FS4SP.ps1 to import into a FS4SP setup. `n`n Example:"+
              " .\export_SP2010_keywords_to_CSV.ps1 -c .\configuration.xml`n`n" ; 
           
Add-PSSnapin Microsoft.Sharepoint.Powershell -ea SilentlyContinue

function checkFile($path)
{
  $status = Test-Path $path
    
  return $status
}

function ProcessConfigFile($configFile)
{
  write-host ("[STATUS] Processing config file : " + $configFile) -ForegroundColor green
      
  $doc = New-Object System.Xml.XmlDocument 
  $doc.Load($configFile) 

  $global:siteid = $doc.config.export_SP2010_CSV.SPsiteURL
  $global:keywordsfile = $doc.config.export_SP2010_CSV.output_keywordsfile
  $global:bestbetsfile = $doc.config.export_SP2010_CSV.output_bestbetsfile
  $global:synonymsfile = $doc.config.export_SP2010_CSV.output_synonymsfile  
  
  write-host ("[STATUS] Using these configurations: `n") -ForegroundColor green
  write-host ("`t site url to export from : " + $siteid) -ForegroundColor green  
  if(checkFile($keywordsfile))  
  { 
    write-host ("`t File allready exists, will overwrite : " + $keywordsfile) -ForegroundColor green ; 
    del $keywordsfile 
  }  
  else 
  { 
    write-host ("`t keywordsfile : " + $keywordsfile) -ForegroundColor green 
  }
  if(checkFile($bestbetsfile))  
  { 
    write-host ("`t File allready exists, will overwrite : " + $bestbetsfile) -ForegroundColor green ; 
    del $bestbetsfile 
  }  
  else 
  { 
    write-host ("`t bestbetsfile : " + $bestbetsfile) -ForegroundColor green 
  }
  if(checkFile($synonymsfile))  
  { 
    write-host ("`t File allready exists, will overwrite : " + $synonymsfile) -ForegroundColor green ; 
    del $synonymsfile
  }  
  else 
  { 
    write-host ("`t synonymsfile : " + $synonymsfile) -ForegroundColor green 
  }
  
  $temp = New-item -itemtype file $keywordsfile  -force
  $temp = New-item -itemtype file $bestbetsfile  -force
  $temp = New-item -itemtype file $synonymsfile  -force
}


write-host ("---------`n ESP - Export keyword tool from SP-2010 --> CSV files v.0.1 ") -ForegroundColor yellow
write-host ("`n This tool exports SP 2010 `n keywords/bestbets/synonyms into CSV files.`n-----`n") -ForegroundColor yellow


if (!$args)
{
  write-host ("You must specificy command line arguments") -ForegroundColor red
  write-host ("Try : -? for more info") -ForegroundColor red
}
else
{  
  
  
  switch($args)
  {
    "-c" { 
       if ($args[1] -ne $null)
       {
           
         write-host ("[STATUS] Will use config file : " + $args[1] +"") -ForegroundColor green
         $global:configFile = $pwd.path + $args[1]
            
         if (checkFile($global:configFile)) 
         { 
           ProcessConfigFile($global:configFile) 
           $global:proceed = $True
         }
         else 
         { 
           write-host ("[Error ] Config file " + $configFile + " does not exist!`n`n") -ForegroundColor red ; 
           $global:proceed = $False
         }                        
       }
       else 
       {           
         write-host ("[Error ] Need to specify the config file...!") -ForegroundColor red ; 
         $global:proceed = "False"
       }
     }    
    "-?" { write-host $global:help ; exit }
  }  
  
  if ($global:proceed -eq "True")
  {
    $keywords = Get-SPEnterpriseSearchQueryKeyword -Site $global:siteid
    
    #Add first line descriptors in all output files
    "SpecialTerm" > $global:keywordsfile
    "SpecialTerm, Synonyms" > $global:synonymsfile
    "SpecialTerm, Title, URL, Description" > $global:bestbetsfile
    
    foreach($keyword in $keywords)
    {
      $keyword.Term >> $global:keywordsfile
          
      $tmpsynonym = $keyword.synonyms | select term
      ($keyword.Term + "," + $tmpsynonym.term) >> $global:synonymsfile
                    
      #Some keywords can be associated with multiple bestbets
      if ($keyword.bestbets.count -eq 1)
      {
        $bestbetsTitle = $keyword.bestbets | select title
        $bestbetsURL = $keyword.bestbets | select url
        $bestbetsDescription = $keyword.bestbets | select description
              
        ($keyword.Term + "," + $bestbetsTitle.title + "," + $bestbetsURL.url + "," + $bestbetsDescription.description) >> $global:bestbetsfile
      }
      else
      {
        foreach($bet in $keyword.bestbets)
        {
          $bestbetsTitle = $bet | select title
          $bestbetsURL = $bet | select url
          $bestbetsDescription = $bet | select description
                
          ($keyword.Term + "," + $bestbetsTitle.title + "," + $bestbetsURL.url + "," + $bestbetsDescription.description) >> $global:bestbetsfile
        }
      }    
    }
    
    write-host ("`n[STATUS] keywordsfile output captured ") -ForegroundColor green
    write-host ("[STATUS] synonyms output captured ") -ForegroundColor green
    write-host ("[STATUS] keywordsfile output captured ") -ForegroundColor green
    write-host ("[STATUS] Job complete! `n`n") -ForegroundColor green
  }
  else
  {
    $global:help
  }
}