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 a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 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 works today as an independent author, trainer and consultant. Jeff is a regular contributor to a variety on online sites, as well as frequent speaker at technology conferences and user groups. Keep up with Jeff and his projects at http://jdhitsolutions.com/blog.

comments powered by Disqus

MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.