How to do it...

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. 

  1. Open your text editor and create a new empty file, and save it as CustomTypes.ps1xml.
  2. 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>
  1. Save the file at a convenient location.
  2. 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
  1. 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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.151.141