Make working with types in PowerShell easier with TypePx

It is often the little things in life that make a big difference.


Isn’t that a beautiful programming example? That wasn’t a heading you just read, it was a piece of code. It’s simple, it’s very expressive, it’s self-documenting, and it’s pretty hard to misunderstand the intent when you look at it.  That’s not PowerShell code though…it’s Ruby.

Over the past year or so I have taken some MOOCs offered by Coursera and edX.  After playing games that I created for my assignments in Python, I started learning more about Ruby.  Ruby is a wonderful, expressive, dynamic programming language with a vibrant community that has a lot in common with PowerShell.  It also offers more functionality than PowerShell does in some areas, and I started missing some of that expressive syntax that I was able to use in Ruby when I would switch back to working in PowerShell.

Here’s how you can accomplish the same task natively in PowerShell:


It doesn’t offer quite the same punch, does it?  Between all of the brackets, the need to call out to Get-Date, and adding negative days to be able to get a timestamp representing 1 week ago, it just isn’t quite as palatable to me.

The beauty of a more mature language like Ruby is that it’s not just about the availability of a week method for numeric classes, nor the availability of an ago method for a time span class.  It’s the availability of years, months, weeks, days, hours, minutes, seconds and milliseconds, all in singular or plural.  It’s the availability of ago to refer to a date in the past, or fromnow to refer to a date in the future.  Want UTC instead of local time?  There’s a inutc method too, as well as so many other cool things.  Small details matter.

Fortunately, like Ruby, PowerShell is also a dynamic language that has all sorts of extensibility built-in, allowing me to continue to enjoy expressive, Ruby-like syntax, even while working in PowerShell.  I know PowerShell’s extensibility quite well, and have been leveraging it since version 1.  These Ruby features inspired me to create a new PowerShell module, bringing some of the method simplicity, elegance, and usefulness from Ruby into PowerShell for the rest of the PowerShell community to enjoy.  I called it TypePx.  It’s open source, and it defines useful type extensions and simplifies type acceleration to reduce much of the typing that would otherwise be required when working with types in PowerShell.  Also, it works on PowerShell 3.0 or later, and it’s available now.

Here are examples of some of the beautiful things you can do with TypePx:

# Get a timespan in a human readable way

# Use relative dates, as mentioned above

# How about using relative dates in a practical way with event logs?
Get-EventLog -LogName System -After (2).Days.Ago
# You can use it when monitoring file age too
dir *.ps*1 | where LastWriteTime -lt (1).year.ago

# Compact (remove null values) from an array
$a = 1,$null,2,$null,3
# Return unique elements in an array
$a = 1,1,2,3,3,4,4,5,6
# Reverse an array
$a = 1,2,3,4
# Slice an array into chunks
$a = 1,2,3,4,5,6
# Flatten a multi-dimensional array
$a = (1,2),(3,4),(5,6)

These are only a few examples of what you can do with TypePx.  Have you ever worked with hashtables and noticed how annoying collection management is when it comes to hashtable values?  June Blender was looking for a solution to this the other day, and TypePx solves that problem.

$ht = @{}
$ht.Add(‘A’,’This is not a collection’)
$ht.AddArrayItem(‘B’,’This is a collection’)
$ht.AddArrayItem(‘B’,’This adds to the collection’)

By now you have probably seen the cool foreach and where “magic” methods that were added to PowerShell as part of version 4.0.  What about version 3.0 though?  TypePx solves that problem too by adding foreach and where methods to PowerShell 3.0 with all of the bells and whistles that are available in PowerShell 4.0.

Here are a handful of other problems that TypePx makes easier:

  • comparing an array to multiple search strings at the same time in a single call;
  • comparing a string to multiple search strings at the same time in a single call;
  • calculating the sum of an array or of a property on a collection of items;
  • accessing the value hidden inside of a SecureString;

TypePx isn’t just about type extensions either.  It’s also about type acceleration.  In fact, it was originally released as my old TypeAccelerator module that I had previously shared on PowerShell Magazine and posted in the PowerShell Resource Gallery, but then I renamed it and included full documentation for the *-TypeAccelerator commands as well as all of the type extension goodness I described above.  Type acceleration support is a great way to improve the quality of your PowerShell code when you are working with long type names.  Rather than explain it here though, you might want to give my article on PowerShell Magazine a read if you haven’t already since it explains what type acceleration is in more detail and gives examples that show why you might want to know more about it.

To download the latest version of TypePx, please refer to the installation instructions in the readme documentation on GitHub.

That wraps up this article.  Please let me know what you think in the comments!

Now if only PowerShell would properly recognize numeric literals as objects so that you could invoke methods and access properties on them without having to use brackets…

Kirk out.


One thought on “Make working with types in PowerShell easier with TypePx

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s