How to format a DateTime in PowerShell
The same as you would in .NET:
$DateStr = $Date.ToString("yyyyMMdd")
Or:
$DateStr = '{0:yyyyMMdd}' -f $Date
The question is answered, but there is some more information missing:
Variable vs. Cmdlet
You have a value in the $Date
variable and the -f
operator does work in this form: 'format string' -f values
. If you call Get-Date -format "yyyyMMdd"
you call a cmdlet with some parameters. The value "yyyyMMdd" is the value for parameter Format
(try help Get-Date -param Format
).
-f
operator
There are plenty of format strings. Look at least at part1 and part2. She uses string.Format('format string', values')
. Think of it as 'format-string' -f values
, because the -f
operator works very similarly as string.Format
method (although there are some differences (for more information look at question at Stack Overflow: How exactly does the RHS of PowerShell's -f operator work?).