PowerShell script to create a new User Profile Property

 

  #Load SharePoint User Profile assemblies

 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
  
 #Function to get service context
 function Get-SPServiceContext([Microsoft.SharePoint.Administration.SPServiceApplication]
  $profileApp)
 {
  $profileApp = @(Get-SPServiceApplication | ? 
  {$_.TypeName -eq "User Profile Service Application"})[0]

return [Microsoft.SharePoint.SPServiceContext]::GetContext

($profileApp.ServiceApplicationProxyGroup,

[Microsoft.SharePoint.SPSiteSubscriptionIdentifier]::Default)

 }
  
 #Get UserProfileManager 
 $serviceContext = Get-SPServiceContext
 $userProfileConfigManager = New-Object Microsoft.Office.Server.UserProfiles.
  UserProfileConfigManager($serviceContext)
 $userProfilePropertyManager = $userProfileConfigManager.ProfilePropertyManager
 $userProfilePropertyManager = $userProfilePropertyManager.GetCoreProperties()

$userProfileTypeProperties = $userProfilePropertyManager.GetProfileTypeProperties

([Microsoft.Office.Server.UserProfiles.ProfileType]::User)

 $userProfileSubTypeManager = [Microsoft.Office.Server.UserProfiles.
  ProfileSubTypeManager]::Get($serviceContext)

$userProfile = $userProfileSubTypeManager.GetProfileSubtype

([Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName

([Microsoft.Office.Server.UserProfiles.ProfileType]::User))

 $userProfileProperties = $userProfile.Properties 
  
 #Set Custom Property values
 $PropertyName = "MyProperty"
 $PropertyDisplayName = "MyCustomProperty" 
 $Privacy="private"
 $PrivacyPolicy="mandatory"
 $coreProperty = $userProfilePropertyManager.Create($false)
 $coreProperty.Name = $PropertyName
 $coreProperty.DisplayName = $PropertyDisplayName
 $coreProperty.Type = "string"
 $coreProperty.Length = "50"
  
 #Add Custom Property
 $userProfilePropertyManager.Add($coreProperty)
 $profileTypeProperty = $userProfileTypeProperties.Create($coreProperty)
  
 #Display Settings
  
 #Show on the Edit Details page
 $profileTypeProperty.IsVisibleOnEditor = $true
  
 #Show in the profile properties section of the user's profile page
 $profileTypeProperty.IsVisibleOnViewer = $true
  
 #Show updates to the property in newsfeed
 $profileTypeProperty.IsEventLog = $true
  
 $userProfileTypeProperties.Add($profileTypeProperty)
 $profileSubTypeProperty = $userProfileProperties.Create($profileTypeProperty)
 $profileSubTypeProperty.DefaultPrivacy =[Microsoft.Office.Server.UserProfiles.Privacy]
  ::$Privacy

$profileSubTypeProperty.PrivacyPolicy =

[Microsoft.Office.Server.UserProfiles.PrivacyPolicy]::$PrivacyPolicy

 $userProfileProperties.Add($profileSubTypeProperty)
  
 #Add New Mapping for synchronization user profile data
 #SharePoint Synchronization connection
 $connectionName ="connectionname"
  
 #Attribute name in FIM/LDAP source
 $attributeName ="attributename"
  
 $synchConnection = $userProfileConfigManager.ConnectionManager[$connectionName]
 $synchConnection.PropertyMapping.AddNewMapping
 ([Microsoft.Office.Server.UserProfiles.ProfileType]::User,$PropertyName,$attributeName)
  
 Privacy is an Enum, which has these values,
 

Privacy Member Name

Description

Public

Everyone

Contacts

Colleagues

Organization

Workgroup

Manager

Manager and me

Private

Me

NotSet

Not set

 

PrivacyPolicy is an Enum, which has these values,

Privacy Policy Name

Description

Mandatory

Makes it a requirement that the user fill in a value.

OptIn

Opt-in to provide a privacy policy value for a property.

OptOut

Opt-out from providing a privacy policy value for a property.

Disabled

Turns off the feature and hides all related user interface

Setting Core Properties - https://msdn.microsoft.com/en-us/library/ee578663.aspx

Add New Mapping - https://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.propertymapcollection.addnewmapping.aspx