PowerShell Date and Time

The need of the scripting never ends, the usage of the date and time in them are always need with perfection. Below are the few PowerShell cmdlets to get the Year, Month, Dates and Time which you could utilize in your reports, scripts and automated emails. 

Year:

Get-Date -UFormat %Y (Year in expanded format e.g., 2020)

Get-Date -UFormat %y (Year in shortened format e.g., 20)




Month:

Get-Date -UFormat %B (Month in expanded format e.g., October)

Get-Date -UFormat %b (Month in shortened format e.g., Oct)

Get-Date -UFormat %m (Month in number e.g., 10 for Oct)

Day:

Get-Date -UFormat %A (Day in expanded format e.g., Sunday)

Get-Date -UFormat %a (Day in shortened format e.g., Sun)






Time:

Get-Date -UFormat %R (Time in 24 Hour format)

Get-Date -UFormat %r (Time in 12 Hour format)






Add or Minus Days and Time:

((Get-Date).AddDays(2)).addHours(-1) - (Add days and Min Hours)

((Get-Date).AddDays(0)).addHours(1)  - (Add Hours)

Short String & Long String for Date and Time:

((Get-Date).AddDays(2)).addHours(-1).ToShortTimeString()
((Get-Date).AddDays(2)).addHours(-1).ToShortDateString()

((Get-Date).AddDays(2)).addHours(-1).ToLongTimeString()
((Get-Date).AddDays(2)).addHours(-1).ToLongDateString()







Comments

Popular posts from this blog

Fitbit Versa 2 - Data not cleared Sync & try again

Fitbit Versa 2 - Unlock with your Phone

Exchange - Mail.que utilizing more space - Exchange Drive - Out of Space