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

PowerShell for the Lazy IT Pro

Posted on June 23, 2014

hammockPowerShell is a great enabler for those of us who don't like to work any harder than we have to. Well, maybe I should say, "for those of us who like to work more efficiently." PowerShell already comes loaded with a number of shortcuts and tricks for getting more done with less effort. Something that I always find frustrating is using object property names later in an expression. Especially property names that are a bit on the long side. For example, here is a typical command:

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!
PS C:\> dir c:\work\*.xml | sort LastWriteTime


    Directory: C:\work


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         12/5/2013   4:19 PM        228 ComputerData.xml
-a---         12/5/2013   5:31 PM       1930 csdata.xml
-a---         12/5/2013   5:33 PM       1336 csdata2.xml
-a---         12/6/2013   9:32 AM       1352 mydata.xml
-a---        12/27/2013  12:39 PM   17060304 WeeklyProcs.xml
-a---        12/30/2013   6:12 PM          3 x.xml
-a---         2/24/2014   4:02 PM       1138 foo.xml
-a---         2/25/2014   9:51 AM      28740 servercoremod.xml
-a---         6/16/2014   1:32 PM     104622 windowtime.xml

I find it tedious to type LastWriteTime. One thing even I keep forgetting is that tab completion will work in this situation. I can start typing the the property name, hit the Tab key and PowerShell should expand the property. This is probably the optimal solution and something I need to make a habit.

Another alternative is to create a shortcut variable.

PS C:\> $lwt = "lastWriteTime"
PS C:\> dir c:\work\*.xml | sort $lwt

The result is the same, and tab completion works on the variable name as well. Using this approach you would define these variables in your PowerShell profile. The third approach is to create your own aliases for commonly used properties.

In PowerShell 3.0 and later you can accomplish this with the Update-TypeData cmdlet.

Update-TypeData -TypeName System.IO.FileInfo -MemberType AliasProperty -MemberName sz -Value Length
Update-TypeData -TypeName System.IO.FileInfo -MemberType AliasProperty -MemberName lwt -Value LastWriteTime

I've just created 2 alias properties for file objects, and yes tab completion will work with these as well.

PS C:\> dir c:\work\*.xml | sort lwt,sz | select name,sz,lwt

Name                                                            sz lwt
----                                                            -- ---
ComputerData.xml                                               228 12/5/2013 4:19:42 PM
csdata.xml                                                    1930 12/5/2013 5:31:48 PM
csdata2.xml                                                   1336 12/5/2013 5:33:32 PM
mydata.xml                                                    1352 12/6/2013 9:32:20 AM
WeeklyProcs.xml                                           17060304 12/27/2013 12:39:52 PM
x.xml                                                            3 12/30/2013 6:12:29 PM
foo.xml                                                       1138 2/24/2014 4:02:18 PM
servercoremod.xml                                            28740 2/25/2014 9:51:04 AM
windowtime.xml                                              104622 6/16/2014 1:32:03 PM

In this case, the aliases help with the sort command but perhaps not so much with the Select-Object portion of the expression. So, I might add some additional aliases, which are going to be more meaningful anyway.

Update-TypeData -TypeName System.IO.FileInfo -MemberType AliasProperty -MemberName Size -Value Length
Update-TypeData -TypeName System.IO.FileInfo -MemberType AliasProperty -MemberName Modified -Value LastWriteTime
Update-TypeData -TypeName System.IO.FileInfo -MemberType AliasProperty -MemberName Created -Value CreationTime

Now I can have the best of everything:

PS C:\> dir c:\work\*.xml | sort lwt,sz | select Name,Size,Created,Modified

Name                                         Size Created                  Modified
----                                         ---- -------                  --------
ComputerData.xml                              228 12/5/2013 2:12:22 PM     12/5/2013 4:19:42 PM
csdata.xml                                   1930 12/5/2013 5:31:48 PM     12/5/2013 5:31:48 PM
csdata2.xml                                  1336 12/5/2013 5:33:32 PM     12/5/2013 5:33:32 PM
mydata.xml                                   1352 12/6/2013 8:45:48 AM     12/6/2013 9:32:20 AM
WeeklyProcs.xml                          17060304 12/27/2013 12:39:33 PM   12/27/2013 12:39:52 PM
x.xml                                           3 12/30/2013 6:12:29 PM    12/30/2013 6:12:29 PM
foo.xml                                      1138 2/24/2014 2:36:39 PM     2/24/2014 4:02:18 PM
servercoremod.xml                           28740 2/25/2014 9:49:59 AM     2/25/2014 9:51:04 AM
windowtime.xml                             104622 6/16/2014 1:32:03 PM     6/16/2014 1:32:03 PM

I put all of the Update-TypeData commands into a script, and dot source it in my profile script. Now I have some shortcuts to save some typing, even more so if I remember to use tab completion, as well as some aliases to give me more meaningful output.

Have a great week and try not to work too hard!


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