SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Im trying to create a userprofile section but when i run the code Im gettiong the following error you cannot cal a method on a null valued expression.please help me out thanks in advance.

[xml]$xmlData = Get-Content "C:\xmlsample.xml"
function CreateUserProfileSection()
      $SiteUrl = "url" 
      $site = Get-SPSite $xmlData.UserProfileSection.SiteURL
      $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
      $xmlData.UserProfileSection.Section | ForEach-Object{
          $property = $pspm.GetSectionByName($_.Name)             
            if($property -eq $null)
                $coreProp = $cpm.Create($true)
                $coreProp.Name = $_.Name
                $coreProp.DisplayName = $_.DisplayName
                  write-host -f green $_.Name section is created successfully             
               write-host -f yellow $_.Name section already exists
share|improve this question
The error is telling you that one of your objects is null (where you may have expected an object) and you are subsequently calling a function on this object. Have a look at the line number and then work out which objects could be null. :) – Russell Jan 5 '12 at 3:42

Make sure you're provided the user running the code with Full Control to the UPA as well as Administrative access to the UPA. You can do this by highlighting the UPA in the Service Applications, then clicking on Permission and Administrators in the ribbon control.

share|improve this answer

What IDE are you using for your powershell? And do you know you can trap the actuall error See this Cheers Patrick

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.