Add Farm Administrator via Powershell

I cant say how many times I’ve had to add myself or another account as a Farm Administrator, and I keep misplacing my scripts.  So lets try my blog, and now it helpful to everyone 🙂

Adding a user to the “Farm Administrators” group effectively will add you to the local machine group WSS_ADMIN_WPG on every server of the SharePoint farm and assigns the SharePoint_Shell_Access role in the SharePoint Configuration database and the Central Administration database.

#################################################
# Add a new Farm Administrator
#################################################
Add-PSSnapin *SharePoint* -erroraction SilentlyContinue
Write-Host
$newFarmAdmin = Read-Host -Prompt 'Enter the new Farm Administrator (i.e. DOMAIN\Username): '
Write-Host
$webApp = Get-SPWebApplication -IncludeCentralAdministration | where-object {$_.DisplayName -eq "SharePoint Central Administration v4"}
$site = $webApp.Sites[0];
$Web = $site.RootWeb;
$farmAdministrators = $web.SiteGroups["Farm Administrators"];
try {
   $farmAdministrators.AddUser($newFarmAdmin, "", $newFarmAdmin, "");
   $contentDB = Get-SPContentDatabase -WebApplication $webApp
   Add-SPShellAdmin -Database $contentDB -Username $newFarmAdmin
   Write-Host "Completed Succesfully!"
} catch {
   Write-Host 'Error: Failed to add user.' -ForegroundColor Red
   Write-Host ('Reason: ' + $_)  -ForegroundColor Red
}
$web.Dispose();
$site.Dispose();

Cheers!