Friday, July 19, 2013

Powershell script to print All User Profiles properties/values - SharePoint User Profile Store

This script prints all User Profile properties/values available inside SharePoint User Profile store.

1.  Loops through all site collection to save usernames to a hash table
2.  Loops through the UserProfiles and output user profile values to log file

Script Usage: .GatherMySiteProfileInfo.ps1 -farm [dev|test|prod] -log

Copy Powershell script as shown below and replace parameters marked in red with proper values:
-----------------------------------------------------------------------------------------------
param($farm, $log)

if($farm -ne $null)
{
    switch($farm)
    {
       "prod" {$SSPName="Enter Prod SSPName"
               $MySiteURL = "https://mysite.sharepointfix.com/"}
       "test" {$SSPName="Enter Test SSPName"
               $MySiteURL = "https://mysite-test.sharepointfix.com/"}
    "dev" {$SSPName="Enter Dev SSPName"
              $MySiteURL = "https://mysite-dev.sharepointfix.com/"}
        default {"`nfarm incorrect, SYNTAX: .GatherMySiteInfo.ps1 -farm [dev|test|prod] -log `n"; exit}
    }
}

#Load the SharePoint assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")| out-null
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")| out-null
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")| out-null

# Setup the UserProfileManager object
try
{
    $ServerContext = [Microsoft.Office.Server.ServerContext]::GetContext($SSPName)
    $UPManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServerContext);
}
catch
{
    Write-Host "Can't access User Profile Manager"
    exit;
}

$logName  = "." + "AllUserProfiles" + "-" + $(Get-Date -Format MM-dd-yyyy-HH-mm) + ".csv"

# Get an enumerator and loop through all the profiles
$enumProfiles = $UPManager.GetEnumerator()

$i = 0
$per = 0
$profileCount = $UPManager.Count;

foreach ($up in $enumProfiles)
{
  #Get all User Profile Property values. You can define your own properties as well
  [String]$userName = $up.Item("Accountname") #example: amjsaito, Needs to be a string so we can parse
  [String]$employeeType = $up.Item("employeetype")
  [String]$orgStatus = $up.Item("organizationalStatus")
  [String]$mgmtCtr = $up.Item("MgtCenterName")
  [String]$costCenName = $up.Item("CostCenterName")
  [String]$costCenNum = $up.Item("CostCenterNumber")
  [String]$building = $up.Item("Building")
  [String]$locationCode = $up.Item("LocationCode")
  [String]$aboutme = $up.Item("AboutMe")
  [String]$resp = $up.Item("SPS-Responsibility")
  [String]$skills = $up.Item("SPS-Skills")
  [String]$projects = $up.Item("SPS-PastProjects")
  [String]$memberships = $up.Item("ProfessionalMemberships")
  [String]$schools = $up.Item("SPS-School")
  [String]$interests = $up.Item("SPS-Interests")
  [String]$pictureURL = $up.Item("PictureURL")
  [String]$myFunction = $up.Item("MyFunction")
  [bool]$hasMySite = $false

  #strip off the domain name
  $URLBreak = $userName.LastIndexOf('');
  $user = $userName.SubString($URLBreak+1);

  Write-Output "`"$username`",`"$employeeType`",`"$orgStatus`",`"$mgmtCtr`",`"$costCenName`",`"$costCenNum`",`"$building`",`"$locationCode`",`"$hasMySite`", `"$aboutme`",`"$resp`",`"$skills`",`"$projects`",`"$memberships`",`"$schools`",`"$interests`",`"$pictureURL`",`"$myFunction`"" | Out-File $logname -append

  $i++

  #if ($i -lt $profileCount) {
    $per = ($i/$profileCount) * 100;
  #}

   $per = "{0:N2}" -f $per
   Write-Progress -Activity "Print All User Profiles" -PercentComplete $per -CurrentOperation "$per% complete" -Status "Looping Through User Profiles"
 }