PowerTip: Use PowerShell to Create a Date String

Summary: Use Windows PowerShell to create a date string that has a month, day, and year.

Hey, Scripting Guy! Question How can I use Windows PowerShell to create a string that represents the date with the month, day, and year?

Hey, Scripting Guy! Answer Feed a pattern of ‘M/d/y’ to the ToString method from Get-Date:

PS C:\> (get-date).ToString(‘M/d/y’)


Comments (8)

  1. JacekZ says:

    Thank You for sharing another great tip.

    Alternative way to generate short date string from datetime:

    (Get-Date).ToString("d")  # nothing new 😉

    but overloaded ToString method supports culture-specific format:

    PS C:> $frfr = 'fr-FR' -as [Globalization.CultureInfo]

    $frca = 'fr-CA' -as [Globalization.CultureInfo]

    $dede = 'de-DE' -as [Globalization.CultureInfo]







  2. Ed Wilson says:

    @JacekZ I am glad you like the tip. Thanks for sharing your overloads for Get-Date.  Nice addition.

  3. Chen V says:

    $culture = New-Object system.globalization.cultureinfo 'en-us'

    $date = Get-Date

    [System.DateTime]::Parse($date, $culture)

    PowerShell rocks!!!

  4. Careful with that, as  date and culture is not fool proof:

    $culture = New-Object system.globalization.cultureinfo 'en-us'

    $date = Get-Date

    [System.DateTime]::Parse($date, $culture)

    Friday, 11 October, 2013 23:35:34


    Sunday, 10 November, 2013 23:35:59

    -check the month & date being switch for a dutch locale v.s. a en-us one-

  5. jrv says:

    @Paul – that only happens to the Dutch for obvious reasons.

  6. @jrv, although I think your remark is meant as a joke, but you may actually be on to something.  A lot of people in the Netherlands tend to simply use the OS in English (IT guys typically in US English). So culture info will be en-US.

    However, we do use European date notation, which is day-month-year rather than month/day/year.

    I've often run into problems with scripting (is VBscript as well as Powershell) because of this.

    p.s. not quite sure what the obvious reasons are?

  7. Mark says: