Link to home
Start Free TrialLog in
Avatar of Joseph Stronks
Joseph StronksFlag for United States of America

asked on

Export users in a SharePoint 2013 site to csv file

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.
ASKER CERTIFIED SOLUTION
Avatar of Piotr Strycharz
Piotr Strycharz
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Joseph Stronks

ASKER

Hi,
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.