Working with dates in PowerShell

I am trying to use the Get-Date cmdlet to get the date of the day. I found the .AddDay (-1) command and it seems to work. The next thing I need to do is retrieve the date in YYMMDD format. This is the part that I cannot figure out how to do this.

This is what I used to get today's date and 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
+3
source share
2 answers

Try the following:

$b = (Get-Date).AddDays(-1).ToString("yyMMdd")
+5
source
$a = Get-Date
$b=$a.AddDays(-1)
$b.ToString("yyMMdd")

(or)

$c = $b.ToString("yyMMdd")
+2
source

Source: https://habr.com/ru/post/1761982/


All Articles