Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Adding Efficiency with PowerShell Type Extensions

Posted on October 31, 2017

The other day I posted an article about custom properties which wrapped up with a look at Update-TypeData. The goal is not so much to make your scripts or modules easier to use, but rather to increase efficiency at the command prompt. When running commands interactively I want to get the information I need as easily as possible. In my PowerShell profile scripts I have code that defines a number of type extensions to make my life easier. I thought I'd share some of them with you.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

It seems I spend a lot of time working with files in PowerShell so I have code to add some type extensions to the System.IO.FileInfo object.  For example, I prefer "Size" to "Length", so it is pretty simple to create an alias property.

Update-TypeData -TypeName System.IO.FileInfo -MemberType AliasProperty -MemberName Size -Value Length -force

But I have several alias properties, so I have a hashtable of aliases and their values that I pipe to Update-TypeData.

$aliases = @{
Size = "length"
Modified = "LastWriteTime"
Created = "CreationTime"
}

$aliases.GetEnumerator() | foreach {
 Update-TypeData -TypeName System.IO.FileInfo -MemberType AliasProperty -MemberName $_.key -Value $_.value -force
}

With these additions I can run commands like this:

image

I used to have aliases like 'sz' for Length and 'lwt' for LastWriteTime when I was feeling especially lazy. But I decided that even that was too far.

I also have extensions for measurement objects. I used to have always run code like this:

dir c:\work -file | Measure-object -Property length -sum |
Select Count,@{Name="SumKB";Expression={$_.sum/1kb}}

image

Again, I want something easier to type. In my profile I define script property type extensions for the measureinfo object.

Update-TypeData -TypeName Microsoft.PowerShell.Commands.GenericMeasureInfo -MemberType ScriptProperty -MemberName SumKB -Value {[math]::Round($this.sum/1KB,2)} -Force
Update-TypeData -TypeName Microsoft.PowerShell.Commands.GenericMeasureInfo -MemberType ScriptProperty -MemberName SumMB -Value {[math]::Round($this.sum/1MB,2)} -Force
Update-TypeData -TypeName Microsoft.PowerShell.Commands.GenericMeasureInfo -MemberType ScriptProperty -MemberName SumGB -Value {[math]::Round($this.sum/1GB,2)} -Force

If I don't specify a sum, the property values are 0.

image

But when I need the value it is there.

image

Which makes it much easier to write my PowerShell commands.

dir c:\scripts -file | group extension | 
Select Count,Name,
@{Name="SizeKB";Expression={($_.group | Measure length -sum).sumkb}} |
sort SizeKB  -Descending | select -first 10

image

Again, these extensions are merely to make my life easier at the console. What makes your life easier?


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d