Windows PowerShell


PowerShell's Get-Date and DateTime

Windows PowerShell's Get-Date and DateTime

Manipulating dates is always tricky.  However, PowerShell's get-Date cmdlet does a wonderful job of interpreting the different international formats for day / month / year.  As a result, what every your computer's country locale, it will be easy to calculate how many days there are to Christmas.

Topics for Windows PowerShell Get-Date and DateTime

 ♣

Introduction to the Get-Date Cmdlet

Normally I like to use a learning progression where we begin with get-Help and examine a cmdlet's parameters, next we look at the properties by piping the cmdlet into get-Member.  Finally, we use the knowledge gained to create PowerShell script to tackle a real-life task.

On this page I am making a change to my usual running order.  Firstly I want to tackle the challenge presented by a real-life task first.  Secondly, I will encourage you to research more methods and properties for the cmdlet get-date.  The result is that you have the skills to undertake other DateTime projects.

How Many Days to Christmas? (or Thanksgiving)

Our challenge is simple enough, to knock-up a simple script which will tell us how many days there are until Christmas, Thanksgiving, or any other date you care to place in a text string.

Here in the UK, our operating systems show dates in the format: dd mmmm yyyy.  Whereas in the United States your locale is displayed as mmmm dd yyyy.  Consequently, I was amazed that PowerShell could convert both "25 December 2009" and "November 26 2009" into date values that it could understand and perform calculations.

Example 1 - Calculate Days to Christmas Using Lots of Variables

In example one I have used several variables that are not strictly necesary, I just wanted to show my thought process in creating this very simple PowerShell script.

# PowerShell Script to display days until Christmas
$DecDate = "25 December 2009"
$NovDate ="November 26 2009"
$Thanksgiving = [system.datetime]$NovDate
$Christmas = [system.datetime]$DecDate

$Today =get-date

$Xmas = ($Christmas.DayOfYear - $Today.DayOfYear)
$Thanks = ($Thanksgiving.DayOfYear - $Today.DayOfYear)
"There are " + $Xmas + " days until " + $DecDate
"There are " + $Thanks + " days until " + $NovDate

Note 1:  You may wish to examine the values for $DecDate and $NovDate, then change the sequence of day month to suit your culture.

Note 2:  It's interesting to see how PowerShell leverages .Net Framework, for example, it employs System.DateTime to convert a text string to a date format.

Note 3:  I keep marvelling that PowerShell could understand both formats dd mmmm yyyy and mmmm dd yyyy.

Example 2 - Production Version of 'How Many Days to Christmas'

"There are " + (([system.datetime]"25 december 2009").DayOfYear - `
(get-date).DayOfYear) + " days until " + "25 December 2009"

Note 1:  This is just one long command.  Observe how the backtick (`) enables the command to overspill onto the second line.

Note 2:  This command needed an extra set of round brackets to surround [system.datetime]"25 December 2009"

Note 3:  It's interesting to see how PowerShell interprets a mixture of "text strings" and date calculations.  As with types of bracket, you need the correct type of "double" speech marks here.

Further Research

I'm hoping that the simple example above will give you ideas for date scripts, which will be useful in your PowerShell projects.  If so, then it's well worth examining get-Date's properties, and in particular its methods.

# Research PowerShell's get-Date
get-Date | get-Member

#Also try
get-Help get-Date -full

Note 1: Normally it's a cmdlet's properties that I am most interested in, but with get-date it's the methods that intrigue me, for example addDays and IsDaylightSavingTime

Bonus Technique - DateTime and ParseExact

Once again, here is PowerShell working with the .Net Framework class DateTime.  ParseExact is a neat method which can convert a text string into its DateTime equivalent.

# ParseExact Change date and time with custom specifier.
$DateString = "Sun 04 Oct 2009 12:30 AM -06:00"
$Format = "ddd dd MMM yyyy h:mm tt zzz"
$Translate = [DateTime]::ParseExact($DateString, $Format, $provider)
Write-Host "$DateString converts to $($result.ToString())."

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

Summary of PowerShell Get-Time and DateTime

Scripting dates is always tricky.  In these examples we can see how PowerShell leverages .Net Framework to convert strings into DateTime values, which it can then use for calculations.

See more PowerShell examples for syntax advice

PowerShell Home  • Syntax  • -f format  • Out-Gridview  • Format-Table  • RegEx  • Get-Date  • WhatIf

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.