Import Users from Excel (CSV) to SharePoint using PowerShell

Requirement: Got a bunch of users and want to add them to SharePoint? Use the script below to bulk add users to the SharePoint group programmatically using PowerShell. This script imports a list of users from Excel. CSV file to SharePoint and add them into appropriate groups.

Solution: Let’s import users from Excel to SharePoint using PowerShell. Here is my CSV file with the list of users and their target groups in SharePoint:

sharepoint import users from excel csv

PowerShell script to Import users from CSV to SharePoint:

Here is the PowerShell to bulk add users to SharePoint groups from a CSV file:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#Variables
$UserListCSV = "C:\UsersToImport.csv"
$SiteURL ="https://Sales.Crescent.com"

# Import the CSV file
$UserList = Import-CSV $UserListCSV #-header("GroupName","UserAccount") - If CSV doesn't has headers

#Get the Web
$Web = Get-SPWeb $SiteURL

#Iterate through each user from CSV file
foreach ($user in $UserList)
{
    #Get the Group and User
    $Group = $web.SiteGroups[$User.GroupName]
    $User = $web.Site.RootWeb.EnsureUser($User.UserAccount)
    #Add user to Group
    $Group.AddUser($User) 

    Write-Host "$($User) Added Successfully!" -ForegroundColor Green
}

#Dispose web object
$Web.Dispose()

Import Bulk Users to SharePoint site from CSV file using PowerShell:

My other CSV file has AccountName (in the format of “Domain\UserName” and “GroupName”).

add user to group powershell sharepoint 2010

PowerShell script to add a user to SharePoint group:

Let’s add a bit of error handling this time.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Import the List of Users from a .csv file,
$UserList= Import-CSV "C:\UserList.csv"
 
#Get the Web
$web = Get-SPWeb "https://sharepoint.crescent.com/sites/marketing"
 
foreach ($Row in $UserList)
{
  #Get the Group
   $Group = $web.Groups[$Row.GroupName]
 
 #Validate the user name
 try
   {
        $user = $web.Site.RootWeb.EnsureUser($Row.AccountName)
   }
   catch [system.exception]
   {
     write-host $_.Exception.Message
   }
 
   #Add user if valid user name is provided
    if($user -ne $null)
    {
       $Group.AddUser($user)
       write-host $user.AccountName
    }
}

#Dispose the web object
$Web.Dispose()

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

3 thoughts on “Import Users from Excel (CSV) to SharePoint using PowerShell

  • What about if we have to add AD group as direct permission (not via SharePoint group) on SharePoint site like (Read or edit).

    Reply
  • Hello, I’m newbie on this, todo this can I run the PS script on the client side or I have to run that scrip on the Sharepoint server?

    Reply
  • I’m trying to do something similar to your post above. I have a spreadsheet that contain a column of user name and another column called About Me. I’m trying to figure out how to import the column called About Me into users profiles on Sharepoint using Power Shell but I have yet to figure it out. Any help would be greatly appreciated

    Reply

Leave a Reply