3

I am trying to use the Get-Date cmdlet to get yesterdays date. I have found the .AddDay(-1) command and that seems to work. The next thing i need to do is extract the date in YYMMDD format. This is that part i can not figure out how to do.

This is what i used to get todays date and the previous day.

 $a = Get-Date
"Day: " + $a.Day
"Month: " + $a.Month
"Year: " + $a.Year
"Hour: " + $a.Hour
"Minute: " + $a.Minute
"Second: " + $a.Second

$b=$a.AddDays(-1)
"Day: " + $b.Day
"Month: " + $b.Month
"Year: " + $b.Year
"Hour: " + $b.Hour
"Minute: " + $b.Minute
"Second: " + $b.Second

2 Answers 2

5

Try this:

$b = (Get-Date).AddDays(-1).ToString("yyMMdd")
Sign up to request clarification or add additional context in comments.

Comments

2
$a = Get-Date
$b=$a.AddDays(-1)
$b.ToString("yyMMdd")

(or)

$c = $b.ToString("yyMMdd")

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.