Adding Users into SharePoint group through Powershell – Issues

Most of you already knew on how to add “Active Directory” users into SharePoint group through Powershell.
Any way, I’ve provided the script here.
if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null)
Add-PSSnapin Microsoft.SharePoint.Powershell
$SiteURL = "<<Site URL>>"
$GroupName = "<<GroupName>>"
$UserNames = "<<User1, User2 >>"
    foreach ($User in $UserNames)
        Set-SPUser -Identity $User -Web $SiteURL -Group $GroupName
The above script works for most of the times but if the user domain starts with / is “IN” , it doesn’t work. It throws the following error in that scenario.
PS C:\Users\XXXXXX> C:\Users\ XXXXXX \Desktop\AddUsers.ps1
Set-SPUser : You must specify a valid user object or user identity.
At C:\Users\ XXXXXX \Desktop\AddUsers.ps1:12 char:19
+ Set-SPUser <<<< -Identity $User -Web $SiteURL -Group $GroupName
    + CategoryInfo : InvalidArgument: (:) [Set-SPUser], PSArgumentException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletSetUser
But, if you try to add the same user through STSADM / People picker, it does resolves and add the user into the SharePoint security group.
stsadm -o adduser -url "<<Site URL>>" -userlogin "<<Domain\account>>" -useremail "<<Email address>>" -group "<<SP Group>>" -username "<<Display Name of the User>>"
 Has anyone experienced similar issue?
This entry was posted in Issues, Powershell, SharePoint and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s