Powershell – Script for Dates for file naming

This script produces the output:

20180606

20180605

For the date of 06/06/2018.

cls
$DaysBack =-1
$Yr = Get-Date -format yyyy
$Mt = Get-Date -format MM
$Dy = Get-Date -format dd
$TodayString = $Yr+$Mt+$Dy
$YD = Get-Date
$YD = $YD.AddDays($DaysBack)
$Lyd = ($YD.Year).ToString()
$Lmd = '0'+ ($YD.Month).ToString()
$Lmd = $Lmd.Substring($Lmd.Length-2)
$Ldy = '0'+ ($YD.Day).ToString()
$Ldy = $Ldy.Substring($Ldy.Length-2)
$YesterdayString = $Lyd+$Lmd+$Ldy
ECHO $TodayString
ECHO $YesterdayString