0

I'm looking for a bit of assistance here. I currently have a Powershell script which adjusts the dates within a file. I'm looking to stop myself having to manually adjust these dates every time. What I need is to replace the date two days ago, with the date from yesterday.

I believe that I'd have to use (Get-Date).AddDays(-1) and (Get-Date).AddDays(-2) but I'm not exactly sure how I'd script this in!

What I currently have:

echo "Adjusting Import Dates"

(Get-Content D:\Temp\Example.txt).replace('20180917', '20180918') | Set-Content  D:\Temp\Example.txt
1
  • Can you please include some sample input and expected output. Is this a file of just dates? Commented Sep 19, 2018 at 11:20

1 Answer 1

3

You could try this:

$yesterday = (Get-Date).AddDays(-1).tostring("yyyyMMdd")
$twodaysago = (Get-Date).AddDays(-2).tostring("yyyyMMdd")
(Get-Content D:\Temp\Example.txt).replace($twodaysago, $yesterday) | Set-Content D:\Temp\Example.txt

You just introduce variables for the two dates and format them to the required date format. There is probably some other way of replacing in files, but the above should work.

Sign up to request clarification or add additional context in comments.

Comments

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.