Here you go:
Function Do-WhatOPAskedFor
{
param
(
[Microsoft.SharePoint.SPList] $list
)
# Please note that this is a bad way of getting all items from the list!
$items = $list.Items
$items | Align-DateFields "Modified" "Date Last Saved"
}
Function Align-DateFields
{
param
(
[string] $dateModifiedFieldName,
[string] $dateLastSavedFieldName,
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[Microsoft.SharePoint.SPListItem] $item
)
begin {}
process
{
[DateTime]$dateLastSaved = $item[$dateLastSavedFieldName];
$item[$dateModifiedFieldName] = $dateLastSaved;
$item.Update();
}
end {}
}
Do-WhatOPAskedFor
will do what you asked for; I'm just bad at naming functions. What it does is takes a SharePoint list, enumerates its items, and sends each item to be processed by a function. The function Align-DateFields
takes a SharePoint list item, extracts what's in the specified field, and saves it to another field.
Let me know if you need any further details!
EDIT:
Here's some code to load up the SharePoint list and pass it to the function above:
# Do this if you can load the SharePoint module (e.g., are in a SharePoint Management Shell)
$web = Get-SPWeb http://yourwebsitehere;
$list = $web.Lists["Your list title here"];
Do-WhatOPAskedFor $list;
$web.Dispose();
# Do this if you only have a regular PowerShell shell:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null;
$site = New-Object Microsoft.SharePoint.SPSite("http://yoursitecollectionhere");
$web = $site.AllWebs | Where { $_.Title -eq "Your site title here" } | Select -First 1
$list = $web.Lists["Your list title here"];
Do-WhatOPAskedFor $list;
$site.Dispose();
In the first one, we use the functions in the SharePoint PowerShell module to grab the SPWeb
, extract the SPList
and pass it to the function. In the second, we have to load the Microsoft.SharePoint
assembly, instantiate an SPSite
object which contains the SPWeb
, grab the SPWeb
, extract the SPList
, and pass it to the function.