Friday, September 7, 2012

Let’s Play with DATE using Powershell.


In scripts the DATE is very useful, in my most of the scripts i always use $DATE variable. This became very handy when you want to save the output of the script to a file name and the file name contains the date as name.

There are few tricks which i always used with Get-Date. let me show few of them,

  1. When i want to add dates in Registry entries or keys, i always tried to convert then to the String

      $date = (Get-Date).toString()

      07-09-2012 12-24-51

2. When i want to specify a one week old date , i use .AddDays Method, You can add days or use minus days in current day.

  • $OneWeekOldDate = (Get-Date).AddDays(-7)

  • 07-09-2012 12-28-13

  • $OneWeekFutureDate = (Get-Date).AddDays(7)

  • 07-09-2012 12-36-25

3.  If we want a customise output of the date as per our requirements we can use a -Format parameter.

    • $dateFormat1 = Get-Date -Format 12-42-37
    • $dateFormat2 = Get-Date -Format dd_mm_yyyy

    • 07-09-2012 12-43-29

    • $dateFormat3 = Get-Date -Format dd-mm-yyyy

    • 07-09-2012 12-44-30

that’s all for now Smile

Happy Playing with the TIME ;o)

Happy Weekend …


Aman Dhally

join aman on facebook Join aman on Linkedin follow aman on Twitter

No comments:

Post a Comment