Question

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

Était-ce utile?

La solution

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

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top