How to get all the user properties using powershell in SharePoint 2010

How to get all the user properties using Powershell in SharePoint 2010

In this article, we will be seeing how to get all the user properties using Powershell in SharePoint 2010.

Get all the user properties

I am getting all the user properties of the current login user on the SharePoint site.

$site = Get-SPSite "https://servername:1111/"
$web = $site.RootWeb
$strLoginName = $web.CurrentUser.LoginName
$serverContext = [Microsoft.Office.Server.ServerContext]::GetContext($site)
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serverContext)
$up = $upm.GetUserProfile($strLoginName)
$propertyColl = $up.ProfileManager.PropertiesWithSection

foreach ($property in $propertyColl) {
    write-host $property.Name
}

Sometimes you may receive the following error when you are trying to run the above-mentioned powershell script.

Powershell script

To resolve this error refer http://www.c-sharpcorner.com/UploadFile/Blogs/4344/

I want to get the particular property value of the current SharePoint site login user. I used the following script to get the custom property value.

$site = Get-SPSite "http://servername:1111/"
$web = $site.RootWeb
$strLoginName = $web.CurrentUser.LoginName
$serverContext = [Microsoft.Office.Server.ServerContext]::GetContext($site)
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serverContext)
$up = $upm.GetUserProfile($strLoginName)
$PRID = $up["PRID"].Value
write-Host $PRID

The same thing can be achieved using C# as shown in the following

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server;

namespace AZUserProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://servername:1111/"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    string strAccountName = web.CurrentUser.LoginName;
                    try
                    {
                        ServerContext serverContext = ServerContext.GetContext(site);
                        UserProfileManager upm = new UserProfileManager(serverContext);
                        UserProfile up = upm.GetUserProfile(strAccountName);
                        string PRID = up["PRID"].Value.ToString();
                        Console.WriteLine("PRID :" + PRID);
                        Console.ReadLine();                              
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
    }
}