PowerShell Get all users in a SP Group from a subsite

Is it possible to get all users in a subsite. I found a script online but I keep getting the URL/ID cannot be found. The same command essentially works for the site collection but it doesn’t work on the subsite.

Get-SPSite http://server/sites/yoursite |
Select -ExpandProperty RootWeb |
Select -ExpandProperty Groups |
Where {$_.Name -EQ “your group name here”} |
Select -ExpandProperty Users |
Select Name, Email

Can someone put me in the right direction? Thank you.

=================

  

 

Groups belongs to Root Site Collection.
– Amal Hashim
Jan 26 ’15 at 21:12

=================

2 Answers
2

=================

The error you get with your code when used for a subsite is because you use Get-Site with a subsite Url.
Instead of Get-SPSite use Get-SPWeb as below:

Get-SPWeb http://server/sites/yoursite |
Select -ExpandProperty Groups |
Where {$_.Name -EQ “your group name here”} |
Select -ExpandProperty Users |
Select Name, Email

The code above will find the group only if its created in that site. To find a group regardless where it’s been created (root site or subsite) you have to use SiteGroups instead of Groups

Get-SPWeb http://server/sites/yoursite |
Select -ExpandProperty SiteGroups |
Where {$_.Name -EQ “your group name here”} |
Select -ExpandProperty Users |
Select Name, Email

  

 

My issue was, like you said, using Get-SPSite instead of Get-SPWeb. I also had to remove the Select -ExpandProperty RootWeb command. Thanks.
– ExceptionLimeCat
Jan 27 ’15 at 15:09

SharePoint Groups information is stored in the Site Collection Level.

You can confirm this by opening -> Site Settings -> People & Groups. Now you can see it open the People & Groups page people.aspx from the Site Collection.