A few days ago I posted an article on using Update-TypeData to provide shortcuts to object properties. These shortcuts might save a few keystrokes typing, especially if you use tab completion. They can also give you more meaningful output. But you can take this even further and save yourself even more typing. How many of you have struggled to type an expression like this:
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
dir c:\work\ -file | where {$_.length -gt 100KB} | Select Name, @{Name="Modified";Expression={$_.lastWritetime}}, @{Name="SizeKB";Expression={[math]::Round($_.length/1KB,2)}} | Sort SizeKB -Descending
If you read the last article, you already know that I can use my alias property shortcuts. But you can also define other types of properties. Let's say I often want to get file sizes in KB, and it is pretty tedious having to use Select-Object all the time with a custom hashtable. Instead I can do this with Update-TypeData:
Update-TypeData -TypeName System.IO.FileInfo -MemberType ScriptProperty -MemberName SizeKB -Value {[math]::Round($this.length/1KB,2)}
When defining a ScriptProperty, the $this variable is used instead of $_. Using my previously created aliases, my command is now much easier to write:
PS C:\> dir c:\work\ -file | where {$_.sz -gt 100KB} | Sort SizeKB -Descending | format-table Name, Modified,SizeKB -AutoSize Name Modified SizeKB ---- -------- ------ WeeklyProcs.xml 12/27/2013 12:39:52 PM 16660.45 SSCERuntime_x64-ENU.msi 2/11/2010 8:36:32 PM 3567.5 SSCERuntime_x86-ENU.msi 2/11/2010 8:36:18 PM 3090 test.exe 1/14/2014 10:43:15 AM 540.54 psight.csv 3/4/2014 10:12:46 AM 508.31 WMIExplorer.exe 5/21/2012 1:32:15 PM 452 test2.exe 5/4/1999 4:33:24 PM 270.27 test2.ext 5/4/1999 4:33:24 PM 270.27 PS4NewFeatures-m3.camproj 3/29/2014 7:13:43 PM 255.65 du.exe 5/29/2014 11:42:40 AM 218.19 chi-hvr2-health-full.htm 1/23/2014 10:01:27 AM 127.96 windowtime.xml 6/16/2014 1:32:03 PM 102.17
With the aliases I can use them anywhere in a PowerShell expression. Let me leave you with one more example:
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
I often need to get the total value of something, but often I need it in a different format such as MB or GB. Now I have it.
PS C:\> dir c:\work\*.xml | where {$_.sz -gt 1kb} | measure -Property Size -Sum Count : 7 Average : Sum : 17199422 Maximum : Minimum : Property : Size SumKB : 16796.31 SumMB : 16.4 SumGB : 0.02 PS C:\> (ps chrome | measure ws -sum).SumGB 1.23
That definitely saves some typing. I should probably do something similar for Maximum and Minimum properties as well.
So, what are you constantly typing? Is it something you could be smarter about? I hope you'll share so I can take advantage of your laziness, I mean, efficiency!