TOGEEK
TOGEEK

Reputation: 741

PowerShell v2.0 - Converting to DateTime

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:

File1.xml 14/07/2016 17:09
File2.xml 15/07/2016 09:32

So I am searching for all files between a specific age range and my criteria is:

Older than:

$Today4am = Get-Date -Hour 4 -Minute 0 -Second 0

But later than

$Yesterday4am = $Today4am.AddDays(-1)

The variables output

$Today4am = 15 July 2016 04:00:00
$Yesterday4am = 14 July 2016 04:00:00

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:

$Filter = gci c:\temp\*.xml |
Where {$_.LastWriteTime -gt $Yesterday4am -and {$_.LastWriteTime-lt $Today4am}}

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."".

So I try to convert the variables to add -Format "dd/MM/yyyy HH:mm" and this outputs the correct format. But converting $Yesterday4am = $Today4am.AddDays(-1) -Format "dd/MM/yyyy HH:mm" produces the error:

Cannot convert value "15/07/2016 04:00" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

How can I get both date formats to agree?

Upvotes: 1

Views: 3971

Answers (2)

Richard
Richard

Reputation: 7000

Try using Get-date again to convert $Yesterday4am to DateTime:

| Where {$_.LastWriteTime -gt (Get-Date $Yesterday4am) -and {$_.LastWriteTime-lt $Today4am}}

And if you're worried about performance you can do the Get-Date conversion outside of the Where operator:

$Yesterday4am = Get-Date $Yesterday4am
$Filter = gci c:\temp\*.xml `
    | Where {$_.LastWriteTime -gt $Yesterday4am -and {$_.LastWriteTime-lt $Today4am}}

Upvotes: 1

Shawn Esterman
Shawn Esterman

Reputation: 2342

I believe this was just an issue with your comparisons. If you run my script below you will see that they are both types "DateTime". If you format them they become strings which will not easily compare. When I ran my Where-Object command below I had no errors. Note: I am using PowerShell version 5.

PS> Write-Host "PowerShell Version: $($PSVersionTable.PSVersion.ToString())"
PowerShell Version: 5.0.10586.122

PS> $Today4am = Get-Date -Hour 4 -Minute 0 -Second 0

PS> Write-Host "`$Today4am is type `"$($Today4am.GetType().FullName)`""
$Today4am is type "System.DateTime"

PS> $Yesterday4am = $Today4am.AddDays(-1)

PS> Write-Host "`$Yesterday4am is type `"$($Yesterday4am.GetType().FullName)`""
$Yesterday4am is type "System.DateTime"

PS> $Filter = Get-ChildItem c:\temp\* | Where-Object -FilterScript { ( $_.LastWriteTime -gt $Yesterday4am ) -and ( $_.LastWriteTime-lt $Today4am ) }

PS> $Filter

Directory: C:\temp


Mode                LastWriteTime         Length Name                                                                                                                                                                                   
----                -------------         ------ ----                                                                                                                                                                                   
-a----        7/14/2016   1:13 PM              0 asdfasd 

Upvotes: 0

Related Questions