Config User Profile Properties using PowerShell Script

*******************SetUserProfileProperty.xml*******************************************

<?xml version=”1.0″ encoding=”utf-8″ ?>
<UserProfileProperty>
<URL>http://dev.mysite.com/</URL&gt;
<Property Name=”SPS-JobTitle” IsVisibleOnEditor=”True”></Property>
<Property Name=”SPS-Skills” UserOverride=”True” IsVisibleOnEditor=”True” IsVisibleOnViewer=”True”></Property>
<Property Name=”SPS-PastProjects” UserOverride=”True” IsVisibleOnEditor=”True” IsVisibleOnViewer=”True”></Property>
<Property Name=”TransitRollup” Privacy=”Public” IsVisibleOnEditor=”True” IsVisibleOnViewer=”True”></Property>
<Property Name=”TransitNumber” Privacy=”Public” IsVisibleOnEditor=”True” IsVisibleOnViewer=”True”></Property>
</UserProfileProperty>

*******************SetUserProfileProperty.ps1*******************************************

#—————-Get the xml file—————————————————————

[xml]$xmlData=Get-Content “SetUserProfileProperty.xml”

#—————-Set Privacy Policies for User Profile properties———————————————
function ConfigUserProfileProperty()
{
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq ‘Microsoft.SharePoint.Powershell’} 
if ($snapin -eq $null)
{
Write-Host “`nLoading SharePoint Powershell Snapin`n”
Add-PSSnapin “Microsoft.SharePoint.Powershell”
}

Write-Host “****************************************************************”
Write-Host “*                                           Configure User Profile Property                                     *”
Write-Host “****************************************************************`n”

$site = Get-SPSite $xmlData.UserProfileProperty.URL
$context = Get-SPServiceContext($site)

 #—————–Create a new object of UserProfileConfigManager—————————-#
[Microsoft.Office.Server.UserProfiles.UserProfileConfigManager] $upcm = new-object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($context);
$pm = $upcm.ProfilePropertyManager

#—————–Create a new object of ProfileSubTypeManager—————————-#

$psm = [Microsoft.Office.Server.UserProfiles.ProfileSubTypeManager]::Get($context)
$p = $pm.GetProfileTypeProperties([Microsoft.Office.Server.UserProfiles.ProfileType]::User)
$ps = $psm.GetProfileSubtype([Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName([Microsoft.Office.Server.UserProfiles.ProfileType]::User))

#—————–Create a new object of ConnectionManger—————————-#

[Microsoft.Office.Server.UserProfiles.ConnectionManager] $cm = $upcm.ConnectionManger
[Microsoft.Office.Server.UserProfiles.PropertyMapCollection] $pmc = $cm[“AD Domain”].PropertyMapping
$pspm = $ps.Properties
$xmlData.UserProfileProperty.Property | ForEach-Object{
$sproperty = $pspm.GetPropertyByName($_.Name)
$property = $p.GetPropertyByName($_.Name)
if($property -ne $null)
{
#### Update Defaul Privacy
$Privacy=$_.Privacy
if($Privacy -ne $null)
{
$sproperty.DefaultPrivacy = [Microsoft.Office.Server.UserProfiles.Privacy]::$Privacy
}

#### Update UserOverridePrivacy
$UserOverride=$_.UserOverride
if($UserOverride -ne $null)
{
$sproperty.UserOverridePrivacy = [System.Convert]::ToBoolean($UserOverride)
}
write-host -f yellow $_.Name “: set allow user override privacy to” $sproperty.UserOverridePrivacy “`n”
$sproperty.Commit();
Start-Sleep -Seconds 1;

#### Update Show on the Edit Detail Page
$IsVisibleOnEditor=$_.IsVisibleOnEditor
if($IsVisibleOnEditor -ne $null)
{
$property.IsVisibleOnEditor = [System.Convert]::ToBoolean($IsVisibleOnEditor)
}
write-host -f yellow $_.Name “: set show on the edit detail page to” $property.IsVisibleOnEditor “`n”
Start-Sleep -Seconds 1;

#### Update Show in the Profile Properties Section of the user’s profile page
$IsVisibleOnViewer=$_.IsVisibleOnViewer
if($IsVisibleOnViewer -ne $null)
{
$property.IsVisibleOnViewer = [System.Convert]::ToBoolean($IsVisibleOnViewer)
}
write-host -f yellow $_.Name “: set show in the profile properties section of the user’s profile page to” $property.IsVisibleOnViewer “`n”
Start-Sleep -Seconds 1;

$property.Commit()
write-host -f green $_.Name “property settings are modified successfully `n”
Start-Sleep -Seconds 2;
}
else
{
write-host -f red $_.Name property does not exists
}
}
Start-Sleep -Seconds 5;

####Add new mapping####

if($pmc["propertyname"].DataSourcePropertyName -ne "fieldnameInAD")
{
#---Add new mapping will replace the original one,use same function for change mapping
$pmc.AddNewMapping([Microsoft.Office.Server.UserProfiles.ProfileType]::User, "propertyname", "fieldnameInAD")
}

$site.Dispose()

} #— end of function—

#—————-Calling the function———————————————

ConfigUserProfileProperty
write-host -f Magenta “Finished”
Start-Sleep -Seconds 50;

Advertisements

4 thoughts on “Config User Profile Properties using PowerShell Script

  1. Hi,

    what if the property already exist, but I just want to change the IsVisibleOnEditor=”True” IsVisibleOnViewer=”True” from True to false.

    I know this is a dump question but is just I know nothing of programming just getting my hands on.

    Tks
    Hugo

    • You just need to set IsVisibleOnEditor = $False and IsVisibleOnViewer=$False
      Then call
      $property.TypeProperty.Commit()
      $property.Commit()

      Here is the sample script:
      $site = Get-SPSite -Limit 1;
      $context = Get-SPServiceContext($site)
      $upcm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($context);
      $ppm = $upcm.ProfilePropertyManager
      $cpm = $ppm.GetCoreProperties()
      $ptpm = $ppm.GetProfileTypeProperties([Microsoft.Office.Server.UserProfiles.ProfileType]::User)
      $psm = [Microsoft.Office.Server.UserProfiles.ProfileSubTypeManager]::Get($context)
      $ps = $psm.GetProfileSubtype([Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName([Microsoft.Office.Server.UserProfiles.ProfileType]::User))
      $pspm = $ps.Properties
      $property = $pspm.GetPropertyByName(“”)
      $property.TypeProperty.IsVisibleOnEditor = $False
      $property.TypeProperty.IsVisibleOnViewer = $False
      $property.TypeProperty.Commit()
      $property.Commit()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s