locked
Update person or group field using powershell RRS feed

  • Question

  • hi,

    There are 2 columns in a list

    1) User (person or group)

    1) Manager( person or group)

    I want to update Manager field based on User column using powershell how to achieve this


    sal


    • Edited by SB Ali Thursday, May 24, 2018 12:39 PM
    Thursday, May 24, 2018 12:38 PM

Answers

  • Hi,

    Try this.

    $site = Get-SPSite -Identity http://sp:12001/
    
    Function getUserManager([string]$userName){
    [void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server");            
    #$site=new-object Microsoft.SharePoint.SPSite("http://sp:12001");            
    $serviceContext = Get-SPServiceContext $site;            
                
    $upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);
    $userProfile = $upm.GetUserProfile($userName);
    return $userProfile["Manager"]
    }
    
    $web = $site.OpenWeb()
    $list=$web.Lists["UserNames"]
    $listItems = $list.Items
    $listItemsTotal = $listItems.Count
    
     if($listItemsTotal -ne 0)
      {
        Write-Host("Number of Items found : "+$listItemsTotal )
        foreach($item in $listItems)
         {       
          $userID =$item["User"].Split("{;#}")[0]
    	  $account=$web.AllUsers.GetById($userID)
    	  $domainName=$account.LoginName.Split("|")[1];
          $managerDomainName=getUserManager($domainName);  
    	  $manager=$web.EnsureUser($managerDomainName)
          $item["Manager"] = $manager;
          $item.Update()
            
         }
      }
    else
     {
      Write-Host("Nothing Found")
     }
    $site.Dispose(); 
     
    
    

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by SB Ali Monday, May 28, 2018 8:44 AM
    Monday, May 28, 2018 1:26 AM

All replies

  • Hi,

    Here is sample PowerShell script for your reference.

    $site = Get-SPSite -Identity http://sp:12001/
    $web = $site.OpenWeb()
    $list=$web.Lists["UserNames"]
    $listItems = $list.Items
    $listItemsTotal = $listItems.Count
    
     if($listItemsTotal -ne 0)
      {
        Write-Host("Number of Items found : "+$listItemsTotal )
        foreach($item in $listItems)
         {       
          $user = $web.EnsureUser("contoso\lee")
            
          $item["Manager"] = $user;
          $item.Update()
            
         }
      }
    else
     {
      Write-Host("Nothing Found")
     }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, May 25, 2018 2:41 AM
  • $sourceWebURL = "http://site.subsite.com"
    $sourceListName = "listname"
    
    $spSourceWeb = Get-SPWeb $sourceWebURL
    $spSourceList = $spSourceWeb.Lists[$sourceListName]
    
    $spSourceItems = $spSourceList.Items
    
    foreach($item in $spSourceItems)
    {
         try					#try block to handle exceptions
           {
    	[Microsoft.SharePoint.SPUser]$spuser = $spWeb.EnsureUser("domain\username")
            $item["FieldName"] = $spuser
            $item.Update();
           }
         catch
           {
    		Write-Host 'Manager for user not found'             #you may add item details in your log
           }
    }

    You can use this code. Instead of domain\username you may have values fetched from some other source like .csv file or array or SharePoint list.

    All the best

    Please mark this reply as answer if this solves your problem.
    Friday, May 25, 2018 8:56 AM
  • Hi Lee,

    The Manager Name should be dynamic not static as below

    I want to loop through each item ( User ) column Name  and update (Manager) Column Name

    $user = $web.EnsureUser("contoso\lee")


    sal

    Sunday, May 27, 2018 7:22 AM
  • Hi,

    Try this.

    $site = Get-SPSite -Identity http://sp:12001/
    
    Function getUserManager([string]$userName){
    [void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server");            
    #$site=new-object Microsoft.SharePoint.SPSite("http://sp:12001");            
    $serviceContext = Get-SPServiceContext $site;            
                
    $upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);
    $userProfile = $upm.GetUserProfile($userName);
    return $userProfile["Manager"]
    }
    
    $web = $site.OpenWeb()
    $list=$web.Lists["UserNames"]
    $listItems = $list.Items
    $listItemsTotal = $listItems.Count
    
     if($listItemsTotal -ne 0)
      {
        Write-Host("Number of Items found : "+$listItemsTotal )
        foreach($item in $listItems)
         {       
          $userID =$item["User"].Split("{;#}")[0]
    	  $account=$web.AllUsers.GetById($userID)
    	  $domainName=$account.LoginName.Split("|")[1];
          $managerDomainName=getUserManager($domainName);  
    	  $manager=$web.EnsureUser($managerDomainName)
          $item["Manager"] = $manager;
          $item.Update()
            
         }
      }
    else
     {
      Write-Host("Nothing Found")
     }
    $site.Dispose(); 
     
    
    

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by SB Ali Monday, May 28, 2018 8:44 AM
    Monday, May 28, 2018 1:26 AM
  • Hi,

    Thanks for sharing,it is working fine and  i just want to know the below function usage on how to use it.

    Function getUserManager([string]$userName){
    [void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server");            
    #$site=new-object Microsoft.SharePoint.SPSite("http://sp:12001");            
    $serviceContext = Get-SPServiceContext $site;           


    sal

    Monday, May 28, 2018 7:32 AM
  • Hi,

    I’m not sure I get your point, the function just get user’s manager based on user profile and had used in the script.

    $managerDomainName=getUserManager($domainName); 

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, May 28, 2018 7:58 AM
  • Hi,

    The below powershell script worked for me and i didn't use the function that is why i was asking

    $siteURL="http://sitename/subsite"
    $web = Get-SPWeb $siteURL
    $list=$web.Lists["TestList"]
    $listItems = $list.Items
    $listItemsTotal = $listItems.Count
    
    
     if($listItemsTotal -ne 0)
      {
        Write-Host("Number of Items found : "+$listItemsTotal )
        foreach($item in $listItems)
         { 
          $manager=""      
          $userID =$item["User"].Split("{;#}")[0]
    	  $account=$web.AllUsers.GetById($userID)
    	  $domainName=$account.LoginName.Split("|")[1];
          $managerDomainName=getUserManager($domainName);
    	  $manager=$web.EnsureUser($managerDomainName)
          $item["Manager"] = $manager;
    
          
    
          if($manager)
          {
    
          $item.Update()
          }
          else
          {
            Write-Host "No Manager"
          }
            
         }
      }
    else
     {
      Write-Host("Nothing Found")
     }
    $site.Dispose(); 
     



    sal

    Monday, May 28, 2018 8:22 AM
  • Hi,

    This don’t make scenes, you could try to test with a different user, or debug the script by PowerGUI to check the actual variables’ value.

    Add SharePoint module for first time.

    Add-PSSnapin "Microsoft.SharePoint.PowerShell"


    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, May 28, 2018 8:33 AM
  • Hi,

    Ok got it, i didn't notice the below line

     $managerDomainName=getUserManager($domainName);


    sal

    Monday, May 28, 2018 8:44 AM