Add user to Farm Admin Group

07/01/2012 21:17

# .DESCRIPTION
#       Add users to the Farm Administrators group
#
# .INPUT
#       - Central Admin port number
#       - user or group
#       - http Central admin URL
#
# .NOTES
#       File Name   : Add_user_farm_admin_group.ps1
#       Author      : Ronald Bruinsma @ iDocs.info
#
# #>
# ##
# # Start of script
# ##
 
#Include the SharePoint cmdlets:
#Add-PsSnapin Microsoft.SharePoint.PowerShell
 
Write-Host 'Add user(s) to Farm Administrator group:'
Write-Host '----------------------------------------'
 
# input:
$caPortNumber =  Read-Host -Prompt 'Port number of Central Admin site'
$URL = Read-Host -Prompt 'URL of the Central Admin site'
    do{
    $userToAdd =  Read-Host -Prompt 'User to add to Farm Admin group <DOMAIN><USERNAME>'
 
    #create a website object:
    $site = new-Object Microsoft.SharePoint.SPSite("https://"+$URL+":"+$caPortNumber)
    $web = $site.RootWeb
 
    #get the Farm administrators group:
    $farmadministrators = $web.SiteGroups["Farm Administrators"]
 
    #add the user to the Farm administartor group:
    $farmadministrators.AddUser($userToAdd,"","New Sharepoint Farm Admins","")
 
    #round up
    Write-Host '----------------------------------------'
    Write-Host "Succesfully added " $userToAdd " to the Farm administrator group on the CA site: " $URL":"$caPortNumber
    Write-Host '----------------------------------------'
    Write-Host 'Current users in Farm Administrator group:'
    foreach ($user in $farmadministrators.users)
    {
    Write-Host $user
    }
    Write-Host '----------------------------------------'
 
    # Add another user to the Farm Administrator group? if yes, start all over again.
    $addAnotherUser = Read-Host 'Add another user to the Farm Administrator group? (Y/N)'
}
# if no, than quit the script
until ($addAnotherUser -eq 'N')
 
#dipose everything:
$web.Dispose()
$site.Dispose()