In this PowerShell SharePoint tutorial, we will discuss how to get all SharePoint users and groups using PowerShell. Also, we will see an example on sharepoint powershell add user to group.
I was working on a project where a user wants to remediate the user permissions in SharePoint by deleting all the users added individually / directly by replacing them with AD user groups in the SharePoint site collection and sub sites iteratively.
In general, owners add users directly in SharePoint with their account name but this will be a major issue when we plan for migration from one version to another. This also causes the problem of hanging resigned or old users in SharePoint.
SharePoint get all users and groups PowerShell
I have implemented the below PowerShell script to list all the users across the site collections to list all the users with their group names iteratively.
try
{
#List all the Users with their Group name, Site name iteratively
$objSiteColl = Get-SPSite "http://sharepoint13:12345/" #Provide Site Collection URL
"Site Name`tGroup Name`tUser Name"
foreach ($objWeb in $objSiteColl.AllWebs)
{
write-host -foregroundcolor yellow "`nWorking on web:" $objWeb.Title
foreach ($objGrp in $objWeb.groups)
{
#"Group: " + $objGrp.name;
$grpName = $objGrp.name
write-host "`nGroup Name: " $grpName -foregroundcolor green
foreach ($objUser in $objGrp.users)
{
$objWeb.Title + "`t" + $objGrp.name +"`t"+ $objUser.name
write-host "Login-ID:: " $objUser.UserLogin -foregroundcolor red
}
}
}
}
catch{
Write-Host "`nError:: $($_.Exception.Message)" -foregroundcolor red -BackgroundColor Yellow
}
How do we execute this PowerShell script?
You can execute this script by following the below steps:
- Log on to SharePoint WFE
- Run SharePoint PowerShell IDE as administrator [Read a PowerShell tutorial on Windows PowerShell ISE]
- Run the script with one of the 2 ways:
- .\listAllUsers.ps1 | Out-File output.txt -> this will generate an output file with tab delimiter
- .\listAllUsers.ps1
OutPut:
Add user to SharePoint group PowerShell
Let us see, how to add user to the SharePoint group using PowerShell.
Steps to add a user into the SharePoint group using PowerShell.
Step 1: Open the SharePoint online management shell and copy the below command into your screen.
Note: Before going to start, make sure you have admin permission to the Office 365 tenant and the SharePoint Online site.
Connect-SPOService -Url https://pikasha12-admin.sharepoint.com/ -credential [email protected]
Next, it will ask you to enter the username and Password to access the site.
Step 2: Here is my SharePoint Online group where I want to add a new user.
Step 3: If your connection gets successful, copy the below command and paste it your screen.
Make sure you provide the correct SharePoint group name and user email id which you want to add into the SharePoint group.
Add-SPOUser -Site "https://pikasha12.sharepoint.com/sites/DLH" -Group "DLH Members" -LoginName [email protected]
Step 4: Now you can see, My new user has been added successfully into the group.
You may like the following PowerShell SharePoint tutorials:
- PowerShell SharePoint Commands
- How to activate publishing feature in SharePoint 2013/2016 using PowerShell Script
- Bulk SharePoint Online Site Collection Creation using PowerShell
- PowerShell Script to deploy WSP Solution in SharePoint 2013
- Show hidden features in SharePoint 2013 using PowerShell
- Create document library sharepoint online PowerShell
- Working with file modification time and date using PowerShell
- Working with PowerShell Date Command (Get-Date)
- Retrieve all list names and list guids from SharePoint online site using PowerShell
Hope this SharePoint tutorial explains, how to get all users and groups using PowerShell in SharePoint 2013/2016. Also, we saw, how to add user to sharepoint group PowerShell.
I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019
Hi, whenever I run, I receive
Error:: The following exception occurred while trying to enumerate the collection: “Access is denied. (Exception f
rom HRESULT: 0x80070005 (E_ACCESSDENIED))”.
what is required to work around this? When I am within sharepoint PS I can run commands (no issue with SP commands within ISE).