Prof. Powershell

Perfect PowerShell Properties Part 2: History

Here's how to check to see how long it's been since a custom property has been modified.

Last time I demonstrated how easy it is to add custom properties in PowerShell. In addition to the custom size properties I created last time, I want to add a property that shows how long it has been since the file was last modified.

PS C:\> Update-TypeData -MemberType ScriptProperty -MemberName ModifiedAge -Value {(Get-Date) - ($this.LastWriteTime)} -TypeName system.io.fileinfo -Force

To use the properties, I have to know that they exist.

PS C:\> dir c:\work -file | Select Name,SizeKB, ModifiedAge | out-gridview -title "Work Files"

I can click on column headings to sort as necessary or filter further in Out-Gridview.

[Click on image for larger view.]  Figure 1.

But I don't want to have to select the properties all the time. One option might be to create my own property set. Unfortunately, Update-TypeData doesn't offer an easy way. If you are using the ISE you will get tab completion for MemberType that includes PropertySet. Sadly, that type isn't a member of the validation set and you will get an error. What you'll have to do is create an XML file with the configuration and import it. You can use code like this:

$xml=@"
<?xml version="1.0" encoding="utf-8" ?>
<Types>
<Type>
<Name>System.IO.FileInfo</Name>
<Members>
<PropertySet>
<Name>Modified</Name>
<ReferencedProperties>
<Name>Name</Name>
<Name>Size</Name>
<Name>ModifiedAge</Name>
<Name>LastWriteTime</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</Type>
</Types>
"@
#define the name and path to ps1xml file

$file = Join-Path -path $home\documents\windowspowershell -child MyFileType.ps1xml

#write the XML to the file
$xml | Out-File -FilePath $file
#update the type data with the new information
Update-TypeData -AppendPath $file

The only way you can see the change is by piping a file object to Get-Member

PS C:\> dir c:\work -file | Get-Member Modified

TypeName: System.IO.FileInfo

Name     MemberType  Definition                                      
----     ----------  ----------                                      
Modified PropertySet Modified {Name, Size, ModifiedAge, LastWriteTime}
Once defined, I can use the property set:
PS C:\> dir c:\work -file | format-table -groupby Directory -Property Modified –auto

You can see an excerpt from the results in Figure 2

[Click on image for larger view.]  Figure 2.

Or I could have used Select-Object. As with the other extensions I've shown you, the property set would need to be defined in your PowerShell profile if you always want it to be available. Next time we'll explore another way to make our PowerShell properties pretty.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events