Question

As far as I know the forward slash isn't a special character in PowerShell, but in this particular situation the forward slash causes a problem.

Write-Host $([DateTime]::Now.ToString("MM/dd yyyy"))

In this case ToString ignores the custom date format (probably because it can't parse it) and outputs date in the default format.

I know how to solve the problem - I can escape the forward slash with a backward slash "MM\/dd yyyy", but I'd like to know why it is necessary.

Was it helpful?

Solution

This is not PowerShell doing it, but the .Net framework. / in a date format is seen as the date separator and will be replaced with the date separator set in the Windows settings. The escape for that is a backslash as you noticed.

Write-Host $([DateTime]::Now.ToString("MM\/dd yyyy"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top