Adding a User to a SharePoint Group 2013

To add a  User to SharePoint group, we can use the following PowerShell commands.

$web = Get-SPWeb http://mysite/site
#Get the group name
$groupName = $web.SiteGroups["Contributors"]
#Assign user to variable
$user = $web.Site.RootWeb.EnsureUser(“domain\userName”)
#Add user
$groupName.AddUser($user)
Write-Host -ForegroundColor blue "User Added Successfully";
 
*Contributors -Group Name
*Ensure User-  It is a method in SharePoint which creates a user even if its not available in SharePoint . but user has to be active in Active Directory.