Updating and extending PowerShell object’s type data

One of the things that I have always liked about PowerShell is that it can provide a wealth of information for its various objects. The Get-VM cmdlet, for instance, is able to retrieve granular information about Hyper-V virtual machines. What a lot of administrators don’t realize is that you can actually add to what PowerShell already knows about an object. Let me show you how it’s done.

Before we can extend a PowerShell object’s type data, we need to know the object’s type name. The easiest way to do this is to use the Get command to retrieve an object, and then pipe the command’s output into the Get-Member cmdlet. Suppose for a moment that you wanted to modify the data that is available for system services. You could type Get-Service | Get-Member. As you can see in the figure below, PowerShell shows you the properties and methods that currently exist for the object (in this case, services), but PowerShell also displays the type name just above the list. In the case of the system services, for example, the type name is System.ServiceProcess.ServiceController.

PowerShell object’s type data

Creating members

You can add type data to a PowerShell object by creating members. These members can be script properties (which means that the value is calculated), or they can be aliases. Even though this sounds somewhat complex, creating members is easier than you might expect.

I recommend that you get started by seeing what members currently exist. Sometimes Microsoft creates members for you. Incidentally, members created by Microsoft are persistent, but the members that you create are only available for the duration of your PowerShell session.

To see what members exist, just map the type name to a variable, and then look at the variable’s members. Here is an example:

$Data = Get-TypeData System.ServiceProcess.ServiceController
$Data.Members

In the case of the system services, there aren’t any preexisting members, but let’s go ahead and create a member of our own. There are a few different pieces of information that you will need to supply when creating a member.

First, as you may have already guessed, you will have to supply the type name. Personally, I like to map the type name to a variable. PowerShell doesn’t require you to use a variable for this purpose, but I find that doing so helps to simplify the command. I will show you an example in a moment.

The next piece of information that you will need to provide is the type of member that you are creating. The valid member types are ScriptProperty and AliasProperty.

The third piece of information that you will have to include is a name for the member that you are creating. You can call the member anything that you want, as long as the name isn’t already being used.

Finally, the fourth piece of information that you have to supply is the value. The value is the information that will be assigned to the member that you are creating. When you supply the value, it must be enclosed in brackets {}.

So now that I have outlined the basic requirements, let’s go ahead and create a member. I am going to stick to working with the system services, just for the sake of consistency.

The first thing that we have to decide is what the new member will do. We can make the member do anything that we want, but I am going to create a member named Computer that includes the name of the computer that the service is running on (in my example, it will be the local machine).

As previously noted, the first step is mapping the type name to a variable. Here is what such a command might look like:

$Data=‘System.ServiceProcess.ServiceController’

Now that we have mapped the type name to a variable, we can use the Update-TypeData cmdlet to create the member. Here is what such a command might look like:

Update-TypeData -TypeName $Data -MemberType ScriptProperty -MemberName Computer -Value {$env:ComputerName}

As you can see, the TypeName simply points to the variable containing my type name. The MemberType is set to script property, which means that I am going to let PowerShell calculate a value. The MemberName is set to Computer, indicating that the custom member will be called Computer. The value is set to $env:ComputerName, which retrieves the computer name from the Windows environment variable.

In contrast, an alias simply assigns a secondary name to a value that already exists. If for example, we wanted to create an alias called Comp that contains the same value as the Computer member, we could do so with a command like this one:

Update-TypeData -TypeName $Data -MemberType AliasProperty -MemberName Comp -Value Computer -Force

As you can see, I changed the member type to AliasProperty, set the member name to Comp, and set the value (which is being forced) to match that of the Computer member that I created earlier. Here is what the command looks like in action.

But did it work?

Now that I have shown you how to create custom members, you may be wondering how to tell if the process worked or not, and how you can use the members that you have created. To verify the existence of the members, just use the command that I showed you earlier:

Get-Service | Get-Member

The figure below lists both of the custom members. Comp is at the very top of the list, and Computer is at the bottom of the list.

PowerShell object’s type data
So how do you use these custom methods? You can use them in exactly the same way as any other object properties. If for example, you type the following command, you will see Computer and Comp listed among the object’s properties.

Get-Service WinDefend | Select-Object *

PowerShell object’s type data

Updating PowerShell object’s type data: One caveat

Keep in mind that the data custom members that you create will only persist for the duration of your PowerShell session. I recommend that you create a script to define any custom members that you want to use on an ongoing basis.

Featured image: Freepik / TechGenix photo illustration

Leave a Comment

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top