title: PowerShell v2.0 - Converting to DateTime
body: <p>I have some date variables, with which to perform queries against files in a specific directory. I can GCI each file no problem, as shown in the example below:</p>
  
  <pre><code>File1.xml 14/07/2016 17:09
  File2.xml 15/07/2016 09:32
  </code></pre>
  
  <p>So I am searching for all files between a specific age range and my criteria is:</p>
  
  <p>Older than:</p>
  
  <pre><code>$Today4am = Get-Date -Hour 4 -Minute 0 -Second 0
  </code></pre>
  
  <p>But later than</p>
  
  <pre><code>$Yesterday4am = $Today4am.AddDays(-1)
  </code></pre>
  
  <p>The variables output</p>
  
  <pre><code>$Today4am = 15 July 2016 04:00:00
  $Yesterday4am = 14 July 2016 04:00:00
  </code></pre>
  
  <p>So the variable pipes out the date in a different format to the file themselves, so I need to ensure both formats are the same in order to do the comparison. Here is the original script:</p>
  
  <pre><code>$Filter = gci c:\temp\*.xml |
  Where {$_.LastWriteTime -gt $Yesterday4am -and {$_.LastWriteTime-lt $Today4am}}
  </code></pre>
  
  <p><em>Bad argument to operator '-gt': Could not compare "14/07/2016 17:09:52" to "14/07/2016 0
  4:00". Error: "Cannot convert value "14/07/2016 04:00" to type "System.DateTime". Error:
   "String was not recognized as a valid DateTime."".</em></p>
  
  <p>So I try to convert the variables to add <em>-Format "dd/MM/yyyy HH:mm"</em> and this outputs the correct format. But converting <em>$Yesterday4am = $Today4am.AddDays(-1) -Format "dd/MM/yyyy HH:mm"</em> produces the error:</p>
  
  <pre><code>Cannot convert value "15/07/2016 04:00" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
  </code></pre>
  
  <p>How can I get both date formats to agree?</p>