Export users in a SharePoint 2013 site to csv file

Sahap Yuzer
Sahap Yuzer used Ask the Experts™
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.

$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)
$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.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software/web developer
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


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

Thank you again.


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