PowerShell: SharePoint - Export Farm and Web Solutions

Updated on 2/26/2015

Script to export farm solutions.

$dirName = "$(pwd)"
Write-Host "Exporting farm solutions to $dirName"
$farm = Get-SPFarm
$solutions = $farm.Solutions
foreach($solution in $solutions)
{
	$filename = $Solution.SolutionFile.Name 
	$solution.SolutionFile.SaveAs("$dirName\$filename")
}

Script to export web solutions.

$web = "http://urltoSPWeb"
Write-Host "Exporting user solutions of web $web to $dirName"
$spWeb = Get-SPWeb -Identity $web
$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::SolutionCatalog
$solGallery = $spWeb.Site.GetCatalog($listTemplate)
$solGallery.Items | foreach{
	$filename = $dirName + "\" + $_.Name.ToString() 
	[System.IO.FileStream]$outStream = New-Object System.IO.FileStream($filename, [System.IO.FileMode]::Create);
	$fileData = $_.File.OpenBinary();
	$outStream.Write($fileData, 0, $fileData.Length);
	$outStream.Close();
}