Wednesday, April 11, 2012

PowerShell: Compare Dates

Today we want to compare an existing date from a SharePoint List called "Compare" with the date of today. We want to check if the date is the same or not.

So here is the code:
$web = Get-SPWeb "YourSite"
$CompareList = $web.lists["Compare"]
$CompareListItem = @($CompareList.items | sort-object {$_["Modified"]} -descending)[0]
[datetime]$Today = [datetime]::Now
[datetime]$StartTime = [datetime]$CompareListItem["Start_Time"]
if($Today.Date.CompareTo($StartTime.Date) -eq 0 )
   $result = Yes
   $result = No
What does it do?

It loads your web, than chooses a list, in this case "Compare". I decided that I only need the newest item. With "[datetime]$Today = [datetime]::Now" we will get the date of today. The next line will get us the Date-Field from the SharePoint List. Because we are using "[datetime]", we can now compare the two dates.

No comments:

Post a Comment