September 23rd, 2015

Extending Objects with PowerShell: Week of the Year

Doctor Scripto
Scripter

Summary: Ed Wilson talks about adding members to an object with Windows PowerShell to get the week of the year.

Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is that it is configurable. Not merely configurable, but amazingly flexible. For example, when I look at a DateTime object, I see the following:

PS C:\Users\mredw> Get-Date | fl *

DisplayHint : DateTime

DateTime    : Tuesday, September 22, 2015 11:48:25 AM

Date        : 9/22/2015 12:00:00 AM

Day         : 22

DayOfWeek   : Tuesday

DayOfYear   : 265

Hour        : 11

Kind        : Local

Millisecond : 230

Minute      : 48

Month       : 9

Second      : 25

Ticks       : 635785193052307708

TimeOfDay   : 11:48:25.2307708

Year        : 2015

There are a lot of properties exposed. The DayOfYear and the DayOfWeek are a couple of useful properties that I might not be expecting. There are also a lot of methods available. I mean, there are a huge number of methods. Everything from adding days and hours to finding out if it is daylight savings time. These methods are shown here:

PS C:\Users\mredw> get-date | Get-Member -MemberType method

   TypeName: System.DateTime

Name                 MemberType Definition

—-                      ———-       ———-

Add                  Method     datetime Add(timespan value)

AddDays              Method     datetime AddDays(double value)

AddHours             Method     datetime AddHours(double value)

AddMilliseconds      Method     datetime AddMilliseconds(double value)

AddMinutes           Method     datetime AddMinutes(double value)

AddMonths            Method     datetime AddMonths(int months)

AddSeconds           Method     datetime AddSeconds(double value)

AddTicks             Method     datetime AddTicks(long value)

AddYears             Method     datetime AddYears(int value)

CompareTo            Method     int CompareTo(System.Object value), int CompareTo…

Equals               Method     bool Equals(System.Object value), bool Equals(dat…

GetDateTimeFormats   Method     string[] GetDateTimeFormats(), string[] GetDateTi…

GetHashCode          Method     int GetHashCode()

GetObjectData        Method     void ISerializable.GetObjectData(System.Runtime.S…

GetType              Method     type GetType()

GetTypeCode          Method     System.TypeCode GetTypeCode(), System.TypeCode IC…

IsDaylightSavingTime Method     bool IsDaylightSavingTime()

Subtract             Method     timespan Subtract(datetime value), datetime Subtr…

ToBinary             Method     long ToBinary()

ToBoolean            Method     bool IConvertible.ToBoolean(System.IFormatProvide…

ToByte               Method     byte IConvertible.ToByte(System.IFormatProvider p…

ToChar               Method     char IConvertible.ToChar(System.IFormatProvider p…

ToDateTime           Method     datetime IConvertible.ToDateTime(System.IFormatPr…

ToDecimal            Method     decimal IConvertible.ToDecimal(System.IFormatProv…

ToDouble             Method     double IConvertible.ToDouble(System.IFormatProvid…

ToFileTime           Method     long ToFileTime()

ToFileTimeUtc        Method     long ToFileTimeUtc()

ToInt16              Method     int16 IConvertible.ToInt16(System.IFormatProvider…

ToInt32              Method     int IConvertible.ToInt32(System.IFormatProvider p…

ToInt64              Method     long IConvertible.ToInt64(System.IFormatProvider …

ToLocalTime          Method     datetime ToLocalTime()

ToLongDateString     Method     string ToLongDateSt ring()

ToLongTimeString     Method     string ToLongTimeString()

ToOADate             Method     double ToOADate()

ToSByte              Method     sbyte IConvertible.ToSByte(System.IFormatProvider…

ToShortDateString    Method     string ToShortDateString()

ToShortTimeString    Method     string ToShortTimeString()

ToSingle             Method     float IConvertible.ToSingle(System.IFormatProvide…

ToString             Method     string ToString(), string ToString(string format)…

ToType               Method     System.Object IConvertible.ToType(type conversion…

ToUInt16             Method     uint16 IConvertible.ToUInt16(System.IFormatProvid…

ToUInt32             Method     uint32 IConvertible.ToUInt32(System.IFormatProvid…

ToUInt64             Method     uint64 IConvertible.ToUInt64(System.IFormatProvid…

ToUniversalTime      Method     datetime ToUniversalTime()

If I want to know how many weeks are left…

Even with all of those methods, if I want to know how many weeks are left in the year—well, it is not there. Strangely enough, Windows PowerShell seems to know this answer anyway. If I use a UFormat of %V (this is case sensitive), I can get the week of the year, and then I can easily subtract it from 52 and find out how many weeks I have left in the current year. The use of the UFormat is shown here:

PS C:\Users\mredw> Get-Date -UFormat %V

38

I would like to have this available as a property. I can do the following, and add it temporarily:

PS C:\Users\mredw> get-date | Add-Member -MemberType NoteProperty -Name WeekOfYear -V

alue (Get-Date -UFormat %V) -PassThru | fl *

DisplayHint : DateTime

WeekOfYear  : 38

DateTime    : Tuesday, September 22, 2015 11:48:36 AM

Date        : 9/22/2015 12:00:00 AM

Day         : 22

DayOfWeek   : Tuesday

DayOfYear   : 265

Hour        : 11

Kind        : Local

Millisecond : 700

Minute      : 48

Month       : 9

Second      : 36

Ticks       : 635785193167005123

TimeOfDay   : 11:48:36.7005123

Year        : 2015

The key “trick” is to use Add-Member to add something to the object. I can add a NoteProperty, and give it the name of WeekOfYear. I then make my value the result of Get-Date –Uformat $V. The essential code is shown here:

get-date | Add-Member -MemberType NoteProperty -Name WeekOfYear -Value (Get-Date -UFormat %V)

Now of course, this does not help too much because it simply displays nothing. That is why I added –PassThru. As shown here, with –PassThru, the output is simply the default display from the Get-Date cmdlet:

PS C:\> get-date | Add-Member -MemberType NoteProperty -Name WeekOfYear -Value (Get-D

ate -UFormat %V) -PassThru

Tuesday, September 22, 2015 1:27:09 PM

Using Format-List * adds the property set to the output, and I can then see the newly created WeekOfYear property.

If I store the results back into a variable, I can use it later, for example:

PS C:\> $dte = get-date | Add-Member -MemberType NoteProperty -Name WeekOfYear -Value

 (Get-Date -UFormat %V) -PassThru

PS C:\> $dte

Tuesday, September 22, 2015 1:29:01 PM

PS C:\> $dte.WeekOfYear

38

I could use it later to find the remaining weeks in the year by subtracting from 52:

PS C:\> 52 – $dte.WeekOfYear

14

That is all there is to using Windows PowerShell to add a number-of-weeks property to a DateTime object. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.