You can use PowerShell Get-Date cmdlet and DateTime methods to format DateTime in PowerShell. Here are few examples for your reference –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
## Get the current date and time Get-Date ## returns 07 October 2021 09:35:16 ## Get the current day Get-Date -Format "dddd" ## returns Thursday Get-Date -Format "dd" ## returns 07 ## Get current month Get-Date -Format "MMMM" ## returns October Get-Date -Format "MM" ## returns 10 ## Get current year Get-Date -Format "yyyy" ## returns 2021 Get-Date -Format "yy" ``## returns 21 ## Get current time Get-Date -Format "HH:mm" ## returns 09:40 Get-Date -Format "HH:mm tt" ## returns 09:40 AM ## Get complete date and time Get-Date -Format "dddd MM/dd/yyyy HH:mm K" ## Get current day of the year $(Get-Date).DayOfYear ## Get current day of the week $(Get-Date).DayOfWeek |