Powershell script to copy a value from one field to another

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 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:

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!

=================

=================

2 Answers
2

=================

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()

  

 

its working. thank you for help. You rock!
– Luis Carvalho
Jan 31 ’15 at 1:27

$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.

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.

  

 

thank you for the clear explanation from a powershell newbie.
– Luis Carvalho
Jan 31 ’15 at 1:28