Showing posts with label null user profile properties. Show all posts
Showing posts with label null user profile properties. Show all posts

Thursday, May 23, 2013

SharePoint 2010/2013: Using PowerShell to Find Empty User Profile Properties

ADD THIS INFORMATION TO YOUR KINDLE LIBRARY!

Check out the whole SharePoint 2013 Solution Series
New Titles Added Weekly!

One of my readers, Mark Cole, needed some assistance on a PowerShell script. Together we crafted a quick solution to find all user profiles in SharePoint that did not have an entry for a specific property. Mark constructed most of the script and I put on some finishing touches.

Essentially we wanted to find all people that did not have a picture associated with their SharePoint User Profile. A user profile's picture information is stored in the PictureURL property. Therefore our example script finds all profiles that do not have an entry in PictureURL.

Here is what we did (full script at bottom of post):

First we needed some variables so we can easily modify the script for other properties or environments:

# Dynamic Settings
$mySiteUrl = "
http://mysite.company.net"
$findProperty = "PictureUrl"


Next, we needed to establish the server context:

# Obtain Context based on site
$mySiteHostSite = Get-SPSite $mySiteUrl
$mySiteHostWeb = $mySiteHostSite.OpenWeb()
$context = Get-SPServiceContext $mySiteHostSite



From the context we can instantiate a ProfileManager object and retrieve all of the SharePoint User Profiles:

# Obtain Profiles from the Profile Manager
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$AllProfiles = $profileManager.GetEnumerator()
$outputCollection = @()



Next, we loop through the profiles and retrieve the account name (for identification purposes) and the property we are interested in finding:

# Loop through profiles and retrieve the desired property
foreach ($profile in $AllProfiles)
{
    $output = New-Object System.Object
    $output | Add-Member -type NoteProperty -Name AccountName -Value $profile["AccountName"].ToString()
    $output | Add-Member -type NoteProperty -Name $findProperty -Value $profile[$findProperty]
    $outputCollection += $output
}


Finally, we list out the collection items that do not have a value for the property (ie. null):
# List all Accounts that do not contain the property
$outputCollection | Where-Object {[bool]$_.($findProperty) -ne $true}




FULL SCRIPT

# Dynamic Settings
$mySiteUrl = "http://mysite.company.net"
$findProperty = "PictureUrl"

Write-Host "Beginning Processing--`n"

# Obtain Context based on site
$mySiteHostSite = Get-SPSite $mySiteUrl
$mySiteHostWeb = $mySiteHostSite.OpenWeb()
$context = Get-SPServiceContext $mySiteHostSite

# Obtain Profiles from the Profile Manager
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$AllProfiles = $profileManager.GetEnumerator()
$outputCollection = @()

# Loop through profiles and retrieve the desired property
foreach ($profile in $AllProfiles)
{
    $output = New-Object System.Object
    $output | Add-Member -type NoteProperty -Name AccountName -Value $profile["AccountName"].ToString()
    $output | Add-Member -type NoteProperty -Name $findProperty -Value $profile[$findProperty]
    $outputCollection += $output
}

# List all Accounts that do not contain the property
$outputCollection | Where-Object {[bool]$_.($findProperty) -ne $true}






 

Matched Content