Read a Date from Text File and Compare with Current Date in Powershell

I pipelined date to a text file through Powershell. Now i need to compare the date in the text file with the current date. I am unable to figure it out. Pls help.

(get-date).AddDays(3) | Out-File -FilePath c:\time.txt
$deadline = Get-Content -Path C:\time.txt
$currenttime = Get-Date
IF ($currenttime -lt $deadline) 
{Write-host "Continue Working"}
Else 
{Write-Host "Your Logic is Wrong"}

I do not get any output. Pls help.

4

1 Answer

You should compare date with date.

(get-date).AddDays(3).ToString() | Out-File -FilePath time.txt
$deadline = [datetime](Get-Content -Path time.txt) 
$currenttime = Get-Date
IF ($currenttime -lt $deadline) {Write-host "Continue Working"} Else  {Write-Host "Your Logic is Wrong"}
0

Your Answer

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

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest