How to format a DateTime in PowerShell
### Formatting a DateTime in PowerShell
PowerShell provides multiple options to format a `[DateTime]` object. Here are a few common methods:
**Using the `ToString()` Method:**
```html
$date.ToString("yyyyMMdd")
```
**Using the `Get-Date` Cmdlet with the `-Format` Parameter:**
```html
Get-Date -Format "yyyyMMdd"
```
**Using Composite Formatting:**
```html
"{0:yyyyMMdd}" -f $date
```
**Using the `GetDateTimeFormats()` Method:**
```html
$date.GetDateTimeFormats()[12]
```
**Using the `-f` Operator (Short for `String.Format`):**
```html
'yyyyMMdd' -f $date
```
**Tips:**
* For a comprehensive list of format strings, refer to the [Microsoft documentation](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings).
* To format the current date in an HTTP header, use the "r" format (RFC1123), but ensure you use `ToUniversalTime()` first.
* You can also use the `-Format` option with the `Get-Date` cmdlet, but it may be less efficient.
* For advanced formatting, consider using composite formatting or the `String.Format()` method from .NET.