title: What is the trick to Where-Object Date Time comparisons?
body: <p>I am writing a script with the intent of creating and updating a csv document with the current list of directories within a share. I will be running it to update the list as new folders are created by a program. I am stuck at trying to get the date time of the last created folder within the csv and comparing it to any newly created folders within the directory. Here is the part I am having problems with:</p>
  
  <pre><code>enter code here
  $csvData = Import-Csv $csvPath
  $lastCreated = $csvData | Select-Object -Last 1
  $directories = Get-ChildItem "\\SomeShare\Dir" -Directory | Where-Object { ([DateTime]$_.CreationTime) -gt ([DateTime]$lastCreated.CreationTime) }
  </code></pre>
  
  <p>The problem is that the "Where-Object" return always includes the last recently created folder from the csv. I would expect this behavior with "Greater Than or Equal To" But as I understand it should only Get-Childitem directories Greater Than the last Creation Time in the csv. I tried casting and not casting the DateTime but it had no effect. What am I missing here to get the Where-Object comparison work and to return only the folders created after the most recent update from my csv? </p>
  
  <p>EDIT:</p>
  
  <p>The csv document is created with this line: </p>
  
  <pre><code>Get-ChildItem -Path "\\SomeShare\Dir" -Directory | Select-Object -Property Name, FullName, CreationTime | Sort-Object CreationTime | Export-Csv -Path $csvPath –NotypeInformation
  </code></pre>
  
  <p>The csv is sorted when created by the Get-ChildItem property “CreationTime” so when I grab –Last 1 it should be the most recently created folder. </p>
  
  <p>Here is an example of the csv data:</p>
  
  <pre><code>"Name","FullName","CreationTime"
  "106008-7","\\SomeShare\Dir\106008-7","3/18/2019 9:21:45 AM"
  "123456","\\SomeShare\Dir\123456","3/18/2019 1:44:50 PM"
  "111222","\\SomeShare\Dir\111222","3/18/2019 2:32:29 PM"
  </code></pre>
  
  <p>Output of $lastCreated.CreationTime is: “3/18/2019 2:32:29 PM"</p>
  
  <p>Here is my Where-Object statement:</p>
  
  <pre><code>$directories = Get-ChildItem "\\SomeShare\Dir" -Directory | Where-Object { ([DateTime]$_.CreationTime) -gt ([DateTime]$lastCreated.CreationTime) }
  </code></pre>
  
  <p>$directories comes out as: "111222","\SomeShare\Dir\111222","3/18/2019 2:32:29 PM"</p>
  
  <p>Or if I add newer directories it comes out as: 
  "111222","\SomeShare\Dir\111222","3/18/2019 2:32:29 PM"
  "111333","\SomeShare\Dir\111333","3/19/2019 9:32:29 AM"</p>
  
  <p>So the baseline time or the time that it should be greater than is always included in the results of the Where-Object. </p>