0

I have the following method to verify the presence of a file in a remote folder where I need to check the date of the file must be the current date.

If I execute the below statement it seems to work

$Exs = Test-Path  '\\srvcld\Homeware\Applications\Processed\Soc*' -NewerThan (Get-Date -UFormat "%d/%m/%Y")

But when it run in the entiere script it returns the following error:

Test-Path : Cannot bind parameter 'NewerThan'. Cannot convert value "02/23/2018" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

Do you know another method that could be used to verify the existence of the files from the current date?

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • You can refer the same topic discussed here https://stackoverflow.com/questions/19774097/finding-modified-date-of-a-file-folder – regi Feb 26 '18 at 11:28
  • Your original code works fine for me with PSv5. `[datetime]::Today` will return a midnight DateTime which should do what you want, but I can't test with v4... `-NewerThan ([datetime]::Today)` – henrycarteruk Feb 26 '18 at 13:09

2 Answers2

1

This might helps you

Get-ChildItem -Path "<path>" | Where-Object {$_.LastWriteTime -ge (Get-Date).Date}
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
DKU
  • 77
  • 1
  • 7
0

Don't format the date. This converts it to a [String], and relies on your culture when converting this back to [DateTime].

Instead, keep is as a [DateTime] object and pass this to Test-Path. That's the type it's expecting anyway:

$Exs = Test-Path  '\\srvcld\Homeware\Applications\Processed\Soc*' -NewerThan (Get-Date).Date

To add to Dharini's answer, this gives the current date but not time. Run the below if you'd like to see the types.


$date1 = Get-Date -UFormat "%d/%m/%Y"
$date2 = (Get-Date).Date

$date1.GetType()     # returns String
$date2.GetType()     # returns DateTime
G42
  • 9,791
  • 2
  • 19
  • 34