Export users in a SharePoint 2013 site to csv file

Sahap Yuzer
Sahap Yuzer used Ask the Experts™
on
Hi,
We need to export members of a SharePoint user group to a text file on a schedule. I found below sites with a script that can do that.
(http://sharepoint-works.blogspot.com/2013/01/export-sharepoint-group-to-excel-using.html)
(https://chaddtalks.wordpress.com/2015/10/05/export-a-list-of-users-from-a-specific-sharepoint-group/)

$SiteUrl="https://portal.site.com";
$GroupName="Site Members";
$Output = @("GroupName|Name|Login|Email|Department|Title")
$web = Get-SPWeb $siteUrl
$site = $web.Site
$rootWeb = $site.RootWeb
$UserList = $rootWeb.Lists["User Information List"]
$web.SiteGroups[$groupName].Users|%{$user = $UserList.GetItemById($_.ID)
if($user -ne $null)
{
$JobTitle = $user["JobTitle"]
$Department = $user["Department"]
}
$Output += ($groupName+"|"+$_.Name+"|"+$_.UserLogin+"|"+$_.Email+"|"+$Department +"|"+$JobTitle)
}
$rootWeb.Dispose()
$web.Dispose()
$site.Dispose()
$Output > "GroupMembers.csv"


When I run the script , it dumps the users to csv file but I'm getting a bunch of below errors. Is there a way to fix the script so it won't have these errors?

You cannot call a method on a null-valued expression.
At C:\export.ps1:9 char:37
+ $web.SiteGroups[$groupName].Users|%{$user = $UserList.GetItemById($_.ID)
+                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software/web developer
Commented:
This might be some language issue.
Do not get user list by title ("User Information List") . Use SPWeb property instead:
$UserList = $rootWeb.SiteUserInfoList

Open in new window

Author

Commented:
Hi,
Thank you very much for your help, it's working perfectly with no errors now.

Thank you again.

Author

Commented:
Your suggestion stopped the errors.

Thank you for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial