0

When I try to run the following powershell script:

Add-PSSnapin Microsoft.Sharepoint.Powershell
[System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$site = new-object Microsoft.SharePoint.SPSite(“http://servername/sites/contoso”)

$web =  Get-SPWeb -Identity http://servername/sites/contoso

$lista =$web.Lists["somelist"]

$items = $lista.items


foreach ($item in $items) {
  $user = $item["A"]

  write-host $useracc 

$useracc =  $web.EnsureUser( $user); 

$item["B"] = $useracc

write-host $useracc

$item.update()
}

$lista.update()

I get the following error:

enter image description here

The source list and destination columns are just plaintext type, so I am puzzled why it is mentioning the user “12” could not be found. 12 is one the a values of the source column.

thanks!

1

As you mentioned both are the plain text and you have value "12" in the text field, that's mean it is not user then you dont need to ensure it. You simply need to copy it.

Try below script,

    Add-PSSnapin Microsoft.Sharepoint.Powershell
    [System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    $site = new-object Microsoft.SharePoint.SPSite("SiteURL”)
    $web =  Get-SPWeb -Identity siteURL
    $lista =$web.Lists["ListName"]
    $items = $lista.items
    foreach ($item in $items) {

    $user = $item["A"]

    $item["B"] = $user

    write-host $user
    $item.update()
    }

    $lista.update()
0
0

$useracc = $web.EnsureUser( $user);

This line of code is checking whether the value of $user is a valid account (see below).

I'm pretty sure you don't have a user named "12". That's the problem.

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.ensureuser.aspx

SPWeb.EnsureUser method

Checks whether the specified logon name belongs to a valid user of the website, and if the logon name does not already exist, adds it to the website.

1
  • thank you for the clear explanation from a powershell newbie. Jan 31 '15 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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