Ev. Ev. - 1 year ago 219
PowerShell Question

How to format a DateTime in PowerShell?

I can format the

commandlet no problem like this:

$date = Get-Date -format "yyyyMMdd"

but once I've got a date in a variable, how do I format it? The statement below

$dateStr = $date -format "yyyMMdd"

returns this error:

"You must provide a value expression
on the right-hand side of the '-f'

Answer Source

Same as you would in .NET.

$DateStr = $Date.ToString("yyyyMMdd")


$DateStr = '{0:yyyyMMdd}' -f $Date
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download