I have an enterprise wiki site collection inside my sharepoint server 2013 on-premises. now i want to write a powershell script, which will send an email mentioning all the wiki pages that have been modified after a specifc date.

so i wrote the following:-

$startdate = (Get-Date 2017-12-19)
$emailbody =""
$web = Get-SPWeb "http://*****/kb/wiki/"
$list = $web.GetList(($web.ServerRelativeUrl.TrimEnd("/") + "/Pages"))  

foreach ($item in $list.items)

if ($item["modified"] -ge $startdate )
    $emailbody = $emailbody + "<br/>" +$item["Title"].toString()



Write-Host "Sending Email"
     #SMTP server name
     $smtpServer = "****"

     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage

     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)

     #Email structure
     $msg.From = "****@******"
     #$msg.ReplyTo = "***@****"
     $msg.subject = "My Subject"
     $msg.body = "Hi , <br/>"+ "The following wiki pages have been modifed since 19/12/2017 <br/>"

     #Sending email

Now currently the problem is that the following check if ($item["modified"] -ge $startdate ) will always return false. Even i have wiki pages which were modified after 19/12/2017.. so can anyone advice on this please?

second question. is there a way to allow specifying the hours:minutes:seconds beside the day/month/year?

  • 1
    Have you checked the value you are getting from returning the date on row 1? By default it returns a long date so you should type (get-date -format d -date 2017-12-19). Secondly, you need to make sure that the date format is the same in SharePoint as it is with your variable startdate. (Check the regional settings). – Christoffer Dec 20 '17 at 14:54
  • 1
    also, worth converting $item["modified"] to date explicitly. – Vamsi K K Dec 20 '17 at 15:04
  • @Christoffer now using (get-date -format d -date 2017-12-19) i will get the following 19/12/2017 and i have a wiki page which have the following last modified date 20/12/2017 11:56 AM but the if statement if ($item["modified"] -ge $startdate ) will be false – john Gu Dec 20 '17 at 15:07
  • @VamsiKK how i can do so ? and if you can mention how i can pass the hour:minute:second ?? – john Gu Dec 20 '17 at 15:07
  • @Christoffer now i try to get the date time as follow $CultureDateTimeFormat = (Get-Culture).DateTimeFormat $DateFormat = $CultureDateTimeFormat.ShortDatePattern $TimeFormat = $CultureDateTimeFormat.LongTimePattern $DateTimeFormat = "$DateFormat $TimeFormat" $DateTime = [DateTime]::ParseExact("19/12/2017 3:14:03 PM",$DateTimeFormat, System.Globalization.DateTimeFormatInfo]::InvariantInfo,[System.Globalization.DateTimeStyles]::None) – john Gu Dec 20 '17 at 15:22

1) You need to first change the date format to ISO8601 DateTime format.

2) Instead of iterating over each list item, you should use CAML Query to filter these out.

Change your code as below:

$startdate = Get-Date 12/19/2017

$startDateIsoFormat = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime($startdate)

$emailbody =""
$web = Get-SPWeb "http://*****/kb/wiki/"
$list = $web.GetList(($web.ServerRelativeUrl.TrimEnd("/") + "/Pages"))  

$spQuery = New-Object Microsoft.SharePoint.SPQuery;
$camlQuery = "<Where><Geq><FieldRef Name='Modified'/><Value Type='DateTime'>$startDateIsoFormat</Value></Geq></Where>";
$spQuery.Query = $camlQuery
$listItems = $list.GetItems($spQuery)

foreach ($item in $listItems)
    $emailbody = $emailbody + "<br/>" +$item["Title"].toString()    

/// rest of your code
  • thanks for the reply. but can you please advice if i can specify the time inside the startdate... to say something as follow $startdate = Get-Date 12/19/2017 14:30:21?? – john Gu Dec 21 '17 at 13:06
  • i tired you code but i got this error $startdate = Get-Date 12/19/2017 Get-Date : Cannot bind parameter 'Date'. Cannot convert value "12/19/2017" to type "System.DateTime". Error: "String was not recognized as a valid DateTime." At line:1 char:23 + $startdate = Get-Date 12/19/2017 – john Gu Dec 21 '17 at 13:27
  • not really sure what could be issue here, can you try it as $startdate = Get-Date 19/12/2017 14:30:21 ? – Gautam Sheth Dec 22 '17 at 6:53
$startdate = "21/12/2017 14:15:01" # string object so you need explict typecast
#startdate = Get-Date # DateTime object   
if (([DateTime]$item["Modified"]) -ge ([DateTime]#startdate)){...}

you can refer here for more PowerShell codes and scenarios as needed.

  • your code raised the following exception Cannot convert value "21/12/2017 14:15:01" to type "System.DateTime". Error: "String was not recognized as a valid DateTime." At line:4 char:5 + if (([DateTime]$item["Modified"]) -ge ([DateTime]$startdate)) – john Gu Dec 21 '17 at 13:03
  • try in this format $startdate = "21/Dec/2017 14:15:01" – little_big_thoughts Dec 21 '17 at 13:37
  • or in this format $startdate = "21-Dec-2017 14:15:01" – little_big_thoughts Dec 21 '17 at 13:40
  • now i tried this code $startdate = "19/Dec/2017 01:15:01" and then i do the check as follow if (([DateTime]$item["Modified"]) -ge ([DateTime]$startdate)).. but the if statement will always return false,, although i have modified many wiki pages after 19/Dec/2017 01:15:01" .. not sure what is going on ? – john Gu Dec 21 '17 at 14:38
  • Then i suspect regional settings datetime conversion overlap check and try below things: 1. If you are executing PowerShell script from EST server but the $item["Modified"] returns datetime of EU timing then you need ti convert the you local time i.e.. EST time where you execute you script to EU time then compare. – little_big_thoughts Dec 21 '17 at 16:38

Replace the code below

if ($item["modified"] -ge $startdate )


if ($item["Modified"] -ge $startdate )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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