kafka kafka - 4 months ago 16
PowerShell Question

Is there a simpler way to work with UK date format in Powershell?

To work with UK date format, as is my preference, the following code snippet will get me a UK date format (Powershell does not take into account machine locale).

$UKdate = [datetime]::Parse($req.ServicePoint.Certificate.GetExpirationDateString(),([Globalization.CultureInfo]::CreateSpecificCulture('en-GB')))


This seems unwieldy, particularly when the language is normally so concise. Is there a better way that I'm not aware of?

Answer

PowerShell does take the locale into account, but casting to [DateTime] does not.

That is, if you were to run this with a UK culture it'll parse it as a UK date:

Get-Date $req.ServicePoint.Certificate.GetExpirationDateString()

Where as this will always choose en-US:

[DateTime]$req.ServicePoint.Certificate.GetExpirationDateString()

Chris