0

Any help is greatly appreciated. Here is the scenario:

I have a powershell script which I am using from another post on stackexchange.

I have two fields in a list, the script copies the value from one field into the other and works great however, I would like to add some conditional logic into the script.

TestPublishedDate gets updated by testupdatedate field however, I would like for TestPublishedDate only to be updated if it is empty.

Here is the script I am using:

#Add SharePoint PowerShell Snapin which adds SharePoint specific cmdlets
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

#Variables that we are going to use for list editing
$listName = "Approvals"

#Get the SPWeb object and save it to a variable
$web = Get-SPWeb http://discoverydev09/

#Get the SPList object to retrieve the "Demo List"
$list = $web.Lists[$listName]


#Get all items in this list and save them to a variable
$items = $list.items

#Go through all items
foreach($item in $items)
{
    $user = $item["testupdatedate"]
    #Change the value of the "Title" column
    $item["TestPublishedDate"] = $user

    #Update the item
    $item.Update()

}

I am sure it is something simple, the list will need to run daily on a list with over 5000 items. If there are any tips on making this script more streamlined please feel free to let me know!

Or even if PnP could be used to make this easier?

Thanks

1 Answer 1

0

Try Using below code:

#Add SharePoint PowerShell Snapin which adds SharePoint specific cmdlets
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

#Variables that we are going to use for list editing
$listName = "Approvals"

#Get the SPWeb object and save it to a variable
$web = Get-SPWeb http://discoverydev09/

#Get the SPList object to retrieve the "Demo List"
$list = $web.Lists[$listName]


#Get all items in this list and save them to a variable
$items = $list.items

#Go through all items
foreach($item in $items)
{
    $user = $item["testupdatedate"]
    #Change the value of the "Title" column
    $testPublishedDate = $item["TestPublishedDate"]

    if(!$testPublishedDate)
    {
        $item["TestPublishedDate"] = $user
    }
    #Update the item
    $item.Update()

}
9
  • hi @Ganesh Sanap Thanks for the response, this works but i am unsure how it works out the site URL etc. Also this still updates the TestPublisheddate even if there is a value, I need it to ONLY update the field if it is empty..
    – Adz127
    Sep 19, 2019 at 9:46
  • You are passing your site URL at line $web = Get-SPWeb http://discoverydev09/? Sep 19, 2019 at 9:48
  • no Problem, Its because i did not join the two scripts. in terms of the conditional logic your help would be appreciated! thanks
    – Adz127
    Sep 19, 2019 at 9:49
  • Yes. You need to merge the code. I have just written for block. Please upvote ( ^ ) and accept it as an answer. So that it will be easy for other users to find the correct answers. Sep 19, 2019 at 9:51
  • 1
    Thanks Ganesh, it worked a charm!
    – Adz127
    Sep 19, 2019 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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