Query date field

24/06/2015 15:52

Gets all pages created until today

Add-PSSnapin microsoft.sharepoint.powershell -ea 0
$webUrl = "https://yourSitecollection"
$listName = "Pages"


$web = Get-SPWeb $webUrl
$list = $web.Lists[$listName]

$value = [DateTime]::Today
$date = Get-Date $value -Format "yyyy-MM-ddTHH:mm:ssZ"
#SPUtility.CreateISO8601DateTimeFromSystemDateTime method to achieve the same.

$query = new-object microsoft.sharepoint.spquery;
$query.Query = "<Where><Leq><FieldRef Name='Created'/><Value Type='DateTime'>"+ $date +"</Value></Leq></Where>";
$items = $list.GetItems($query)

$items.Count


Or you can query date directly, compared to today's date

    <Where>
        <Geq>
            <FieldRef Name="MyDateColumn" />
            <Value Type="DateTime">
                <Today OffsetDays="-5" />
            </Value>
        </Geq>
    </Where>
 
In case of need to include time, extend Value tag

 <Value Type='DateTime' IncludeTimeValue='True'></Value>