1

So I am trying to get the date 3 months back using the Powershell command Get-Date. However, when I try to add multiple get-dates in one line it errors out or doesn't give me the results I'm looking for.

The End result I'm trying to get is $checkDate = 6-7-2016

I've tried this, but it doesn't really work:

$checkDate = (Get-Date).month -3 "-" (Get-Date).day "-" (Get-Date).year 

Any ideas on how to accomplish this? I'm newer to Powershell and not exactly sure how to concatenate properly.

I'm using PS 4.0

1 Answer 1

6

What you are trying to do is format a calculated date. So let's start with calculating the date:

(Get-Date).AddMonths(-3)

That gets you today's date minus 3 months. Next you want to format it in a specific manner. That being Month-Day-Year. That can be done as such:

(Get-Date).AddMonths(-3).ToString("M-d-yyyy")

That results in:

6-7-2016
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.