Formatting PowerShell Get-Date inside string Formatting PowerShell Get-Date inside string powershell powershell

Formatting PowerShell Get-Date inside string


"This is my string with date in specified format $($theDate.ToString('u'))"

or

"This is my string with date in specified format $(Get-Date -format 'u')"

The sub-expression ($(...)) can include arbitrary expressions calls.

MSDN Documents both standard and custom DateTime format strings.


You can use the -f operator

$a = "{0:D}" -f (get-date)$a = "{0:dddd}" -f (get-date)

Spécificator    Type                                Example (with [datetime]::now)d   Short date                                        26/09/2002D   Long date                                       jeudi 26 septembre 2002t   Short Hour                                      16:49T   Long Hour                                       16:49:31f   Date and hour                                   jeudi 26 septembre 2002 16:50F   Long Date and hour                              jeudi 26 septembre 2002 16:50:51g   Default Date                                    26/09/2002 16:52G   Long default Date and hour                      26/09/2009 16:52:12M   Month Symbol                                    26 septembrer   Date string RFC1123                             Sat, 26 Sep 2009 16:54:50 GMTs   Sortable string date                            2009-09-26T16:55:58u   Sortable string date universal local hour       2009-09-26 16:56:49ZU   Sortable string date universal GMT hour         samedi 26 septembre 2009 14:57:22 (oups)Y   Year symbol                                     septembre 2002

Spécificator    Type                       Example      Output Exampledd              Jour                       {0:dd}       10ddd             Name of the day            {0:ddd}      Jeu.dddd            Complet name of the day    {0:dddd}     Jeudif, ff, …        Fractions of seconds       {0:fff}      932gg, …           position                   {0:gg}       ap. J.-C.hh              Hour two digits            {0:hh}       10HH              Hour two digits (24 hours) {0:HH}       22mm              Minuts 00-59               {0:mm}       38MM              Month 01-12                {0:MM}       12MMM             Month shortcut             {0:MMM}      Sep.MMMM            complet name of the month  {0:MMMM}     Septembress              Seconds 00-59              {0:ss}       46tt              AM or PM                   {0:tt}       ““yy              Years, 2 digits            {0:yy}       02yyyy            Years                      {0:yyyy}     2002zz              Time zone, 2 digits        {0:zz}       +02zzz             Complete Time zone         {0:zzz}      +02:00:               Separator                  {0:hh:mm:ss}     10:43:20/               Separator                  {0:dd/MM/yyyy}   10/12/2002


Instead of using string interpolation you could simply format the DateTime using the ToString("u") method and concatenate that with the rest of the string:

$startTime = Get-DateWrite-Host "The script was started " + $startTime.ToString("u")