title: Formatting PowerShell Get-Date inside string
body: <p>I can't get my head around how formatting a datetime variable inside a string works in PowerShell.</p>
  <pre><code>$startTime = Get-Date
  Write-Host &quot;The script was started $startTime&quot;
  
  # ...Do stuff...
  
  $endTime = Get-Date
  Write-Host &quot;Done at $endTime.  Time for the full run was: $( New-TimeSpan $startTime $endTime).&quot;
  </code></pre>
  <p>gives me the US date format while I want <a href="http://en.wikipedia.org/wiki/ISO_8601" rel="noreferrer">ISO 8601</a>.</p>
  <p>I could use</p>
  <pre><code>$(Get-Date -Format u)
  </code></pre>
  <p>but I want to use $endTime to make the calculation of the timespan correct.</p>
  <p>I have tried all permutations of <code>$</code>, <code>(</code>, <code>)</code>, <code>endTime</code>, <code>-format</code>, <code>u</code>, <code>.ToString(...)</code> and <code>.ToShortDate()</code>, but the one that works.</p>