{"id":13599186,"url":"https://github.com/jdhitsolutions/PSTypeExtensionTools","last_synced_at":"2025-04-10T12:31:40.142Z","repository":{"id":145020994,"uuid":"109034656","full_name":"jdhitsolutions/PSTypeExtensionTools","owner":"jdhitsolutions","description":"A set of PowerShell tools for working with type extensions.","archived":false,"fork":false,"pushed_at":"2024-10-12T13:54:19.000Z","size":215,"stargazers_count":44,"open_issues_count":0,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-06T07:52:58.306Z","etag":null,"topics":["json","powershell","powershell-scripting","scripting","typedata","xml"],"latest_commit_sha":null,"homepage":null,"language":"PowerShell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jdhitsolutions.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","contributing":null,"funding":".github/FUNDING.yml","license":"License.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":"https://paypal.me/jdhitsolutions?locale.x=en_US"}},"created_at":"2017-10-31T18:10:16.000Z","updated_at":"2024-10-23T06:13:45.000Z","dependencies_parsed_at":"2024-10-28T17:17:17.724Z","dependency_job_id":null,"html_url":"https://github.com/jdhitsolutions/PSTypeExtensionTools","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdhitsolutions%2FPSTypeExtensionTools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdhitsolutions%2FPSTypeExtensionTools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdhitsolutions%2FPSTypeExtensionTools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdhitsolutions%2FPSTypeExtensionTools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jdhitsolutions","download_url":"https://codeload.github.com/jdhitsolutions/PSTypeExtensionTools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248217130,"owners_count":21066633,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","powershell","powershell-scripting","scripting","typedata","xml"],"created_at":"2024-08-01T17:01:00.639Z","updated_at":"2025-04-10T12:31:38.470Z","avatar_url":"https://github.com/jdhitsolutions.png","language":"PowerShell","funding_links":["https://paypal.me/jdhitsolutions?locale.x=en_US"],"categories":["PowerShell"],"sub_categories":[],"readme":"# PSTypeExtensionTools\n\n[![PSGallery Version](https://img.shields.io/powershellgallery/v/PSTypeExtensionTools.png?style=for-the-badge\u0026logo=powershell\u0026label=PowerShell%20Gallery)](https://www.powershellgallery.com/packages/PSTypeExtensionTools/) [![PSGallery Downloads](https://img.shields.io/powershellgallery/dt/PSTypeExtensionTools.png?style=for-the-badge\u0026label=Downloads)](https://www.powershellgallery.com/packages/PSTypeExtensionTools/)\n\nThis PowerShell module contains commands that make it easier to work with type extensions. Many of these commands are wrappers for built-in tools like [Get-TypeData](http://go.microsoft.com/fwlink/?LinkId=821805) or [Update-TypeData](http://go.microsoft.com/fwlink/?LinkId=821871). This module should work in Windows PowerShell 5.1 and PowerShell 7.x.\n\n## Release\n\nYou can install the current release from the PowerShell Gallery:\n\n```powershell\nInstall-Module PSTypeExtensionTools\n```\n\n## Why You Want to Use This\n\nLet's say you want to update a number object, but you have no idea what the type name is. Once you have read help for the commands in this module, you could run a PowerShell command like this:\n\n```powershell\n123 | Get-PSType | Add-PSTypeExtension -MemberType ScriptProperty -MemberName SquareRoot -Value {[math]::Sqrt($this)}\n```\n\nUse `$this` to reference the object instead of `$_`. Now you can get the new property.\n\n```dos\nPS C:\\\u003e $x = 123\nPS C:\\\u003e $x.SquareRoot\n11.0905365064094\n```\n\nOnce you know the type name, you can add other type extensions.\n\n```powershell\nAdd-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Squared -value {$this*$this}\nAdd-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Cubed -value {[math]::Pow($this,3)}\nAdd-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Value -value {$this}\nAdd-PSTypeExtension -TypeName system.int32 -MemberType ScriptMethod -MemberName GetPercent -value {Param([int32]$Total,[int32]$Round=2) [math]::Round(($this/$total)*100,$round)}\n```\n\nHere's how it might look:\n\n```dos\nPS C:\\\u003e $x = 38\nPS C:\\\u003e $x | select *\n\n      SquareRoot Squared Cubed Value\n      ---------- ------- ----- -----\n6.16441400296898    1444 54872    38\n\nPS C:\\\u003e $x.GetPercent(50)\n76\nPS C:\\\u003e $x.GetPercent(100)\n38\nPS C:\\\u003e $x.GetPercent(110,4)\n34.5455\n```\n\n### Go GUI\n\nAs an alternative to the command-line, you can use the native `Show-Command` cmdlet to display a graphical interface.\n\n```powershell\nShow-Command Add-PSTypeExtension\n```\n\n![Add via GUI](images/show-add.png)\n\nClicking Run will insert this code at your prompt.\n\n```powershell\nAdd-PSTypeExtension -MemberName ToTitleCase -MemberType ScriptMethod -TypeName System.String -Value { (Get-Culture).TextInfo.ToTitleCase($this.ToLower())}\n```\n\nIf you like this extension, you can export it and re-import it later.\n\n## Get Type Extensions\n\nTo see current type extensions, you can use `Get-PSTypeExtension`. You can choose to see all extensions or selected ones by member name. CodeProperty extensions are hidden by default.\n\n```dos\nPS C:\\\u003e Get-PSTypeExtension system.int32\n\n   TypeName: System.Int32\n\nName       Type           Value\n----       ----           -----\nSquareRoot ScriptProperty  [math]::Sqrt($this)\nSquared    ScriptProperty  $this*$this\nCubed      ScriptProperty  [math]::Pow($this,3)\nValue      ScriptProperty  $this\nGetPercent ScriptMethod   Param([int32]$Total,[int32]$Round=2) [math]::Round(($this/$total)*100,$round)\n```\n\nIf you always want these extensions, you would have to put the commands into your PowerShell profile script. Or you can export the extensions to a JSON or XML file. You can either export all members or selected ones, which is helpful if you are extending a type that already has type extensions from PowerShell.\n\n```powershell\nGet-PSTypeExtension system.int32 |\nExport-PSTypeExtension -TypeName system.int32 -Path c:\\work\\int32-types.json\n```\n\nIn your PowerShell profile scrip,t you can then re-import the type extension definitions.\n\n```powershell\nImport-PSTypeExtension -Path C:\\work\\int32-types.json\n```\n\n## PSTypeExtensionTools Cmdlets\n\n### [Add-PSTypeExtension](docs/Add-PSTypeExtension.md)\n\nAdd a new type extension such as an `Alias` or `ScriptProperty`.\n\n### [Export-PSTypeExtension](docs/Export-PSTypeExtension.md)\n\nExport type extensions to a json, xml or ps1xml file.\n\n### [Get-PSType](docs/Get-PSType.md)\n\nGet the type name of an object.\n\n### [Get-PSTypeExtension](docs/Get-PSTypeExtension.md)\n\nGet type extensions for a given type.\n\n### [Import-PSTypeExtension](docs/Import-PSTypeExtension.md)\n\nImport type extension definitions from a JSON file or XML.\n\n### [New-PSPropertySet](docs/New-PSPropertySet.md)\n\nIn addition to custom properties, PowerShell also has the idea of a _propertyset_. This allows you to reference a group of properties with a single name.\n\nLet's say you have loaded the sample 'System.IO.FileInfo` type extensions from this module.\n\n```powershell\nImport-PSTypeExtension -Path $PSTypeSamples\\fileinfo-extensions.json\n```\n\nYou could write a command like this:\n\n```powershell\ndir c:\\work -file | Select-Object Name,Size,LastWriteTime,Age\n```\n\nOr you could create a custom property set. These have to be defined in `ps1xml` files. The `New-PSPropertySet` simplifies this process.\n\n```powershell\nNew-PSPropertySet -Typename System.IO.FileInfo -Name FileAge -Properties Name,Size,LastWriteTime,Age -FilePath d:\\temp\\FileInfo.types.ps1xml\n```\n\nI've included the file in the Samples folder.\n\n```dos\nPS C:\\\u003e Update-TypeData $PSTypeSamples\\FileInfo.types.ps1xml\nPS C:\\\u003e dir c:\\work -file | Select-Object FileAge\n\nName                          Size    LastWriteTime            Age\n----                          ---- -  -----------              ---\na.dat                            42   2/12/2024 5:36:55 PM     23.17:27:21\na.txt                         14346   12/31/2023 9:10:15 AM    67.01:54:00\na.xml                        171394   12/31/2023 12:15:44 PM   66.22:48:32\naa.ps1                        28866   12/31/2023 9:13:16 AM    67.01:51:00\naa.txt                        28866   12/31/2023 9:11:18 AM    67.01:52:58\nabout.json                    16455   2/27/2024 10:11:03 AM    09.00:53:12\nabout_ADReportingTools         1688   3/4/2024 7:37:01 PM      03.15:27:14\nb.csv                          1273   11/13/2023 12:11:35 PM   114.22:52:40\n...\n```\n\nIf your property set is using custom properties, you need to load them into your PowerShell session before you can use the property set.\n\n## Create ps1xml Files\n\nThe `Export-PSTypeExtension` command will also export extensions to a properly formatted .ps1xml file. This can be useful when building type extension files for a module where you want to use the traditional ps1xml form. You can also import these types of files with `Update-TypeData` with the `-AppendPath` or -PrependPath parameters.\n\nWhen exporting to .ps1xml file, `Export-PSTypeExtension` has a dynamic parameter, `Append`. This allows you to combine multiple type extensions into a single file. If you intend to use a property set, create that file first. Then append your custom type extensions to that file.\n\nHere's how this might look.\n\nFirst, create a property set file.\n\n```powershell\nNew-PSPropertySet -Typename System.IO.FileInfo -Name TimeSet -Properties \"Name\",\"Length\",\"CreationTime\",\"LastWriteTime\" -FilePath c:\\work\\file.types.ps1xml\n```\n\nI'll define a few type extensions.\n\n```powershell\nAdd-PSTypeExtension -TypeName System.IO.FileInfo -MemberType AliasProperty -MemberName Size -Value Length\nAdd-PSTypeExtension -TypeName System.IO.FileInfo -MemberType ScriptProperty -MemberName ModifiedAge -Value {New-TimeSpan -Start $this.LastWriteTime -End (Get-Date)}\n```\n\nI'll even add a second property set to the same file using these new extensions.\n\n```powershell\nExport-PSTypeExtension -TypeName System.IO.FileInfo -MemberName Size,ModifiedAge -Path c:\\work\\file.types.ps1xml -append\n```\n\nI'll end up with this file:\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003c!--\nThis file was created with New-PSPropertySet from the\nPSTypeExtensionTools module which you can install from\nthe PowerShell Gallery.\n\nUse Update-TypeData to append this file in your PowerShell session.\n\nCreated 03/09/2024 15:27:56\n--\u003e\n\u003cTypes\u003e\n  \u003cType\u003e\n    \u003cName\u003eSystem.IO.FileInfo\u003c/Name\u003e\n    \u003cMembers\u003e\n      \u003cPropertySet\u003e\n        \u003cName\u003eTimeSet\u003c/Name\u003e\n        \u003cReferencedProperties\u003e\n          \u003cName\u003eName\u003c/Name\u003e\n          \u003cName\u003eLength\u003c/Name\u003e\n          \u003cName\u003eCreationTime\u003c/Name\u003e\n          \u003cName\u003eLastWriteTime\u003c/Name\u003e\n        \u003c/ReferencedProperties\u003e\n      \u003c/PropertySet\u003e\n      \u003cPropertySet\u003e\n        \u003cName\u003eAge\u003c/Name\u003e\n        \u003cReferencedProperties\u003e\n          \u003cName\u003eName\u003c/Name\u003e\n          \u003cName\u003eSize\u003c/Name\u003e\n          \u003cName\u003eLastWriteTime\u003c/Name\u003e\n          \u003cName\u003eModifiedAge\u003c/Name\u003e\n        \u003c/ReferencedProperties\u003e\n      \u003c/PropertySet\u003e\n      \u003cAliasProperty\u003e\n        \u003cName\u003eSize\u003c/Name\u003e\n        \u003cReferencedMemberName\u003eLength\u003c/ReferencedMemberName\u003e\n      \u003c/AliasProperty\u003e\n      \u003cScriptProperty\u003e\n        \u003cName\u003eModifiedAge\u003c/Name\u003e\n        \u003cGetScriptBlock\u003eNew-TimeSpan -Start $this.lastwritetime -End (Get-Date)\u003c/GetScriptBlock\u003e\n      \u003c/ScriptProperty\u003e\n    \u003c/Members\u003e\n  \u003c/Type\u003e\n\u003c/Types\u003e\n```\n\nIn PowerShell, I can load this file and use it.\n\n```powershell\nPS C:\\\u003e Update-TypeData c:\\work\\file.types.ps1xml\nPS C:\\\u003e Get-ChildItem -path c:\\work\\*.csv |\nSort-Object -property size -Descending | Select Age\n\nName              Size LastWriteTime          ModifiedAge\n----              ---- -------------          -----------\nupdates.csv    4021821 11/14/2023 9:00:48 AM  115.06:45:35.2595780\npart5.csv         7332 2/27/2024 6:10:11 PM   9.21:36:12.4672428\nipperf.csv        5008 11/4/2023 11:36:20 AM  125.04:10:03.4641251\nlocalusers.csv    1480 2/27/2024 4:39:32 PM   9.23:06:51.7431393\nb.csv             1273 11/13/2023 12:11:35 PM 116.03:34:48.0298279\nfoo.csv           1077 11/13/2023 12:40:04 PM 116.03:06:19.3069112\ny.csv              524 11/19/2023 2:11:44 PM  110.01:34:39.0826388\nyy.csv             524 12/1/2023 11:28:03 AM  98.04:18:20.7080948\nc.csv              334 11/13/2023 11:58:15 AM 116.03:48:08.3898463\na.csv                0 12/1/2023 11:30:55 AM  98.04:15:27.9106911\n```\n\nI can put the `Update-TypeData` command in my PowerShell profile to always have these extensions. Or I could share the file.\n\n## I Want to Try\n\nYou can find sample and demonstration type extension exports in the [Samples](./samples) folder. When you import the module, this location is saved to a global variable, `$PSTypeSamples`.\n\n```powershell\nPS C:\\\u003e Get-ChildItem $PSTypeSamples\n\n\n    Directory: C:\\scripts\\PSTypeExtensionTools\\samples\n\n\nMode                LastWriteTime         Length Name\n----                -------------         ------ ----\n-a----       12/15/2023   2:25 PM            766 cimlogicaldisk-extensions.json\n-a----        9/28/2024   9:48 AM            265 datetime-extensions.json\n-a----       12/15/2023   5:09 PM            232 eventlog-type.json\n-a----        2/18/2024   1:18 PM           1266 fileinfo-extensions.json\n-a----       11/13/2023   8:37 AM            901 int32-types.json\n-a----        11/1/2023   6:18 PM            653 measure-extensions.json\n-a----       11/13/2023   8:49 AM            890 process-types.xml\n-a----       12/15/2023   6:09 PM            628 README.md\n-a----       12/15/2023   2:09 PM           1246 stringtypes.json\n-a----        11/9/2023  12:08 PM           3024 vm-extensions.json\n\nPS C:\\\u003e Import-PSTypeExtension $PSTypeSamples\\measure-extensions.json -Verbose\nVERBOSE: Starting: Import-PSTypeExtension\nVERBOSE: Importing file C:\\scripts\\PSTypeExtensionTools\\samples\\measure-extensions.json\nVERBOSE: Processing ScriptProperty : SumKB\nVERBOSE: Creating scriptblock from value\nVERBOSE: Performing the operation \"Adding ScriptProperty SumKB\" on target \"Microsoft.PowerShell.Commands.GenericMeasureInfo\".\nVERBOSE: Processing ScriptProperty : SumMB\nVERBOSE: Creating scriptblock from value\nVERBOSE: Performing the operation \"Adding ScriptProperty SumMB\" on target \"Microsoft.PowerShell.Commands.GenericMeasureInfo\".\nVERBOSE: Processing ScriptProperty : SumGB\nVERBOSE: Creating scriptblock from value\nVERBOSE: Performing the operation \"Adding ScriptProperty SumGB\" on target \"Microsoft.PowerShell.Commands.GenericMeasureInfo\".\nVERBOSE: Ending: Import-PSTypeExtension\n\nPS C:\\\u003e Get-ChildItem D:\\VMDisks\\ -file -recurse | Measure-Object -property length -sum |\nselect Count,SumGB\n\nCount   SumGB\n-----   -----\n    4 50.2031\n```\n\nThis project was first described at \u003chttp://jdhitsolutions.com/blog/powershell/5777/a-powershell-module-for-your-type-extensions\u003e\n\nThere is an about help topic you can read:\n\n```powershell\nhelp about_PSTypeExtensionTools\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdhitsolutions%2FPSTypeExtensionTools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdhitsolutions%2FPSTypeExtensionTools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdhitsolutions%2FPSTypeExtensionTools/lists"}