We need an XML file. You can either use the New-Item cmdlet to create one, or simply use your favourite text editor. We use Visual Studio Code for this recipe.
- Open your text editor and create a new empty file, and save it as CustomTypes.ps1xml.
- Add the following content to the XML file. Ensure not to change the case.
<?xml version="1.0" encoding="utf-8" ?>
<Types>
<Type>
<Name>System.IO.FileInfo</Name>
<Members>
<AliasProperty>
<Name>Modified</Name>
<ReferencedMemberName>LastWriteTime</ReferencedMemberName>
</AliasProperty>
<ScriptProperty>
<Name>Age</Name>
<GetScriptBlock>[math]::Round(((Get-Date) - $this.LastWriteTime).TotalDays)</GetScriptBlock>
</ScriptProperty>
<NoteProperty>
<Name>ItemType</Name>
<Value>File</Value>
</NoteProperty>
</Members>
</Type>
</Types>
- Save the file at a convenient location.
- Back at the Terminal (or the PowerShell Integrated Console), enter the following command to update the type data using the XML. Notice the PrependPath parameter.
PS> Update-TypeData -PrependPath ~/Documents/code/github/powershell/chapter-06/CustomTypes.ps1xml
- Now, list out all the files within any directory of your choice.
Get-ChildItem . -File | Select-Object Name, Length, Age, Modified
You should see the new properties that you created.
It is advised to never modify the PS1XML files in the $PSHome directory. They are digitally signed by Microsoft and could be replaced with new versions during upgrades or patches.