Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Extending PowerShell with Custom Property Sets

Posted on November 8, 2017

If you've been following along on the blog recently you've read about my use of PowerShell type extensions. This is a way of adding new properties to things I use all the time. The goal is to save typing and get what I need with minimal effort. You can also take this a step further by creating your own property sets.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

You probably didn't know it but PowerShell already uses property sets. Here's an example you can try yourself.

image

Where did that come from? Pipe Get-Process to Get-Member and see for yourself.

image

A property set is a shortcut way of selecting a subset of properties. You can also define property sets. Let's take my Hyper-V extensions that allow me to run a command like this:

image

I may want to see this group of properties often. But I clearly don't want to type this all the time. You can define a new property set with the Update-TypeData cmdlet, but it will require a little more effort. Unfortunately, there are no easy parameters to use. Instead you need to create a ps1xml file like this:

<?xml version="1.0" encoding="utf-8" ?>
<Types>
   <Type>
        <Name>Microsoft.HyperV.PowerShell.VirtualMachine</Name>
         <Members>
            <PropertySet>
                <Name>PSConfig</Name>
                <ReferencedProperties>
                    <Name>VMName</Name>
                    <Name>State</Name>
                    <Name>DynamicMemoryEnabled</Name>
                    <Name>DiskPath</Name>
                    <Name>TestVHD</Name>
                    <Name>ConfigurationFile</Name>
                </ReferencedProperties>
            </PropertySet>
        </Members>
    </Type>
</Types>

Hopefully this shouldn't be too difficult to read. I'm defining a property set member for the VirtualMachine type with a name of PSConfig.  This property set will use the referenced property names. You can add definitions for other types in the same XML file but I'm not so I named this file MyHyperV.types.ps1xml. The name doesn't really matter but the pattern is to use types.ps1xml as part of the file name.

To load this file I add this line to my PowerShell profile script.

Update-TypeData -AppendPath c:\scripts\MyHyperV.types.ps1xml

I'm not defining anything that conflicts with the out-of-the-box definitions so it doesn't matter if I prepend or append. Now my life is much easier.

image

I can see the new definition with Get-Member.

image

The XML can be a little daunting at first but once you have the outline it should be a simple matter of cutting and pasting. Just remember to watch your tags and case.

Does this look like something you'd use?


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

5 thoughts on “Extending PowerShell with Custom Property Sets”

  1. A wizened Azure Admin says:
    November 8, 2017 at 2:12 pm

    Nice work. Thanks for your contributions to the craft.

  2. Philip says:
    November 9, 2017 at 3:25 am

    It actually looks very handy, and i might consider using it where useful.

    But what i use a lot are calculated properties, which i presume cannot be used in this format?

    1. Jeffery Hicks says:
      November 9, 2017 at 8:40 am

      I believe that is the case. You would first define the property extensions as I did in a previous post. Then you can define the property set using those new properties.

  3. Joseph says:
    November 9, 2017 at 1:12 pm

    I see referenced properties, which I assume, only includes existing properties. Can new custom properties be defined in the psconfig file?

    1. Jeffery Hicks says:
      November 9, 2017 at 6:32 pm

      Property sets reference properties you created earlier. The set is just a list.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d