Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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 2010" and "November 26 2010" into date values that it could understand and perform calculations.

Example 1 - Calculate Days Until Christmas Using Lots of Variables

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

# PowerShell DateTime Script to display days until Christmas
$DecDate = "25 December 2011"
$NovDate ="November 24 2011"
$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 locale.

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.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

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

# PowerShell DateTime Example
"There are " + (([system.datetime]"25 December 2011").DayOfYear - `
(Get-date).DayOfYear) + " days until " + "24 December 2011"

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

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

Note 6:  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.

a) Get-Date Properties

# Research PowerShell's Get-Date Properties
Get-Date | Get-Member

Note 7:  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.

b) Get-Date Parameters

# Research PowerShell's Get-Date Parameters
Clear-Host
Get-Help Get-Date -full

Thanks to PowerShell's help I learned about -Uformat.

c) Formatting Get-Date

Challenge:  UK readers may appreciate employing this -Uformat parameter to rearrange days and months:

# PowerShell Get-Date Format Example
Clear-Host
$Now = Get-Date -Uformat "%A, %d:%m:%Y"
Write-Host "Today in the UK is $Now enjoy your day!"

Guy Recommends:  Solarwinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

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 $($translate.ToString())."

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 PowerShell's DateTime values, which it can then use for calculations.

If you like this page then please share it with your friends

 


See more PowerShell examples for syntax advice

PowerShell Tutorials   • Syntax   • Get-Verb   • PowerShell Nouns   • Get-Member   • Get-Credential

PowerShell -as   • Comparison operators  • Conditional operators 

Get-Date  • Quotes   • Windows PowerShell

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

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.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

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

Please report a broken link, or an error.