0

I have txt file with date value, line by line

enter image description here

I try to compare them to today date in powershell but its not working

$DateTimeNow = (Get-Date).ToString('dd/MM/yyyy')
$data2 = get-content "output.txt"
$z= @()
foreach($line2 in $data2)
{

  if($line2 -match $DateTimeNow){
       write-host "same date"
    }

} 

the compare with "match" not work, I have try -eq and = but nothing better. Have you any idea what I am doing wrong ?

1
  • please, DO NOT post images of code/error-text/sample-data. why? lookee ... >>> Why not upload images of code/errors when asking a question? - Meta Stack Overflow — meta.stackoverflow.com/questions/285551/… Commented Aug 31, 2020 at 17:14

1 Answer 1

4

The input dates all use 2-digit notation for the year (20 for 2020), but your string representing today's date uses 4-digits. Change to the appropriate format and it will work:

$DateTimeNow = Get-Date -Format 'dd/MM/yy'
Sign up to request clarification or add additional context in comments.

1 Comment

@guillaumezac it took me a few takes to spot it too ^_^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.