I would like to know how to add a SharePoint user in a SharePoint group. This snippet is working for some accounts and doesn't not for some others. Why ?

Note: When I using the SharePoint UI to add the not working accounts within the desired group, it's working. I just have to use the Check the names button to find the corresponding account.

$webUrl = "http://www.sitecollection/site";
$groupeName = "Groupe name";
$compte = "DOMAIN\account"; 
$web = Get-SPWeb $webUrl;    
$user = Get-SPUser -web $web.url -Identity $compte -ErrorAction SilentlyContinue;               
$group = $web.SiteGroups[$groupeName];  

Exception calling "AddUser" with "1" argument(s): user doesn't exist or is not unique.

  • You don't need Get-SPUser. Try: $user = $web.EnsureUser($compte)
    – Bunzab
    Commented Jun 12, 2017 at 14:55
  • Thank you for replying. It's still not working. With or without Get-SPUser.
    – ameliapond
    Commented Jun 12, 2017 at 15:02

1 Answer 1


The user record will create into User Information List on the first-time login. When you execute the statement Get-SPUser if a user is not found it returns null. And you are executing $web.EnsureUser($user) on null value.

You can change your code as follows.

$user = $web.EnsureUser($compte); //$compte is user login name
$group = $web.SiteGroups[$groupeName];
  • Thank you for replying and for the advice. Unfortunately, it doesn't change anything :'(
    – ameliapond
    Commented Jun 12, 2017 at 15:43
  • 1
    Can you print user details after EnsureUser statement to make sure this returns user object Commented Jun 12, 2017 at 15:46
  • I printed it and it displays 2 columns UserLogin and Displayname with the expected value inside.
    – ameliapond
    Commented Jun 13, 2017 at 7:29
  • If it is returning expected value then it should add to the group. I will verify this again on my server. Commented Jun 13, 2017 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.