SharePoint 2010/2013 Powershell: Get SP User Properties of all the Site User or a Using Loginname

Here is how you fetch  user properties/info using powershell. Normally to do that we use the following Powershell command but this required a SPUserPipeBind user identity which looks like this i:0#.w|domainusername

Get-SPUser [[-Identity] ] -Web  [-AssignmentCollection ] [-Group ] [-Limit ]

But incase you dont have SPUserPipeBind you can use the following script.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null 
$site = new-object Microsoft.SharePoint.SPSite("http://SiteCollectionAddress")
$web = $site.openweb()
$searchedUser = $web.EnsureUser("DomainLoginName")
write-host "Hello", ";", $searchedUser.DisplayName
$web.Dispose()
$site.Dispose()

Now here comes the compelete script which took me couple of hours to build. For instance you are required to fetch all the users of the site along with their managers, following script is going to do the magic for you. What it does is to

  • Fetch all the User of a specific site collection.  
  • Loop through all the user and fetch each user’s Manager 
  • Using $web.EnsureUser fetches Manager’s Display name since Login Name is not that user friendly. 
  • At the end it takes all the users along with their respective Managers and sends them to a text file.
  • Since the user names were in german in my case so the text file was encoded to  [System.Text.Encoding]::Unicode. You can leave out the third argument while outputting things to text file if you don’t need that. 
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite("http://sitecollection");
$web = $site.openweb()
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
$AllProfiles = $ProfileManager.GetEnumerator()
write-host "Display Name ; Manager's Name ; Manager's Login; "
$output = $null;
foreach($profile in $AllProfiles)
{
$DisplayName = $profile.DisplayName
$ManagerDisplayName = $null;
$Manager = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::Manager].Value
if(![string]::IsNullOrEmpty($Manager))
{
Try
{
$ManagerDisplayName = $web.EnsureUser($Manager).DisplayName
}
Catch
{
$ManagerDisplayName = $Manager
}
}

$output += $DisplayName + ";", $ManagerDisplayName + ";" + $Manager ;
$output += [Environment]::NewLine;
}
[System.IO.File]::WriteAllText("c:UsersWithManagers.txt", $output, [System.Text.Encoding]::Unicode);
write-host "All Set!"
$web.Dispose()
$site.Dispose()

Disclaimer: I don’t completely own this script and the bad new is that I could not keep track of all the sources I followed. Still following are few of them:

http://stackoverflow.com/questions/3896258/how-to-output-text-without-newline-in-powershell
http://blog.incworx.com/blog/sharepoint-administrators-blog/get-a-users-id-using-powershell
http://sharepoint.stackexchange.com/questions/92017/get-user-by-loginname-using-powershell

Vaqar Hyder

E: vaqar.hyder3567@gmail.com

A:

Siebenb�rgen Stra�e 2/1, Korntal-M�nchingen, 70825, PK

Hire me on Freelancer.com

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *