Format Date Time to your Output Needs
If you want to format the date and assign the string to a variable.
I have combined both PowerShell and .NET to provide the flexibility.
$oDate = '{0}' -f ([system.string]::format('{0:yyyyMMddHHmmss}',(Get-Date)))
How this Works
- PowerShell Operator - '{0}' -f (.....)
- .NET Notation - [system.string]::format('customformat',InputObject)
- Customised Format by combining PowerShell with .NET - '{0:yyyyMMddHHmmss}'
- Input Object provided by PowerShell cmdlet - (Get-Date)
- Stored in the PowerShell variable - $oDate
Example
If the date and time when run was Monday, 5 July 2021 5:45:22 PM (Format '{0:F}').
Using the Code
You can customise the the string to meet your requirements by modifying 'yyyMMddHHmmss' using the Microsoft .NET Custom Date Time Notation.