SharePoint 2013: PowerShell script to create a new User Profile Property

$site = new-object Microsoft.SharePoint.SPSite(“https://contoso.com”);
$serviceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);

$userProfileConfigManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($serviceContext)
$profilePropertyManager = $userProfileConfigManager.ProfilePropertyManager
$corePropertyManager = $profilePropertyManager.GetCoreProperties()
$profileTypePropertyManager = $profilePropertyManager.GetProfileTypeProperties([Microsoft.Office.Server.UserProfiles.ProfileType]::User)
$profileSubTypeManager = [Microsoft.Office.Server.UserProfiles.ProfileSubTypeManager]::Get($serviceContext)
$defaultSubType = [Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName([Microsoft.Office.Server.UserProfiles.ProfileType]::User)
$profileSubType = $profileSubTypeManager.GetProfileSubtype($defaultSubType)
$profileSubTypePropertyManager = $profileSubType.Properties

#Create Core Property
$PropertyName = “Supervisor”
$PropertyDisplayName = “Supervisor”
$Privacy=”private”
$PrivacyPolicy=”mandatory”
$coreProperty = $corePropertyManager.Create($false)
$coreProperty.Name = $PropertyName
$coreProperty.DisplayName = $PropertyDisplayName
$coreProperty.Type = “string”
$coreProperty.Length = “50”
$corePropertyManager.Add($coreProperty)

#Create Profile Property
$profileTypeProperty = $profileTypePropertyManager.Create($coreProperty)
$profileTypeProperty.IsVisibleOnEditor = $false
$profileTypeProperty.IsVisibleOnViewer = $true
$profileTypeProperty.IsEventLog = $false
$profileTypePropertyManager.Add($profileTypeProperty)

#Create Profile Sub Property
$profileSubTypeProperty = $profileSubTypePropertyManager.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
$connectionName =”BDC”
$attributeName =”Supervisor”
$synchConnection = $userProfileConfigManager.ConnectionManager[$connectionName]
$synchConnection.PropertyMapping.AddNewMapping([Microsoft.Office.Server.UserProfiles.ProfileType]::User,$PropertyName,$attributeName)

Leave a Reply

Your email address will not be published. Required fields are marked *