PowerShell classes - Part 2: Properties

In the last post of my series about PowerShell classes I explained how you can create your own classes and how you can instantiate custom objects. In this post, I will discuss class properties and show you how to use, read and set them.
Latest posts by Stéphane van Gulick (see all)

A property is a value that adds information to an object. A PowerShell object is essentially a collection of properties and methods (discusses in my next post) which are also called the members of the class.

Assigning a property value ^

You set the value of a property as demonstrated in the example below:

Note that first we created an instance using the new() keyword. As discussed in my previous post, this is called instantiating a class. To read a property you simply add the property name to the object's variable separated with a point:

You can validate parameters in a similar way as in advanced functions in PowerShell. I have updated the class from my previous post with advanced parameter validation, which gives a more precise control on the inputs of our properties.

In the class above I defined six parameters are available to use. With the help of a regular expression I determined that $FirstName and $LastName only accepts ASCCII characters as input and $EmployeeType can either be "Internal" or "External.

We are using the same techniques that we have already been using in functions, workflows or even DSC with parameter validation attributes such as validatePattern or ValidateSet. You can use them all except for "ValidateScript", which doesn't seem to work in PowerShell 5.

The property attribute hidden ^

In the example above the property $UserName has been set to "hidden." If we look a bit closer at our members (properties and methods of the object), by piping the object to Get-Member, we notice that our hidden property $UserName is in fact invisible.

The hidden property is invisible

The hidden property is invisible

Not being visible, doesn’t mean that is not available. We can see that it is still present using the -Force switch.

Displaying hidden object properties

Displaying hidden object properties

The values of hidden properties can be set and read just like visible properties.

The property attribute static ^

The keyword "static" is used for properties that don't change while your script runs (for instance, constants). A key difference to non-static properties is that you can access their values without creating an instance of the class.

[Math]::Pi  is a typical example that refers to the number π. You can access the value of this member of the Math class without instantiating a corresponding object.

To access a static property, you have to use :: operator which the static member accessor in PowerShell.

Accessing a static property

Accessing a static property

As you can see in my example, it is possible set a property has both hidden and static.

In the next example I show you how to create an instance of our Employee class and populate all its properties with values:

Setting and reading properties

Setting and reading properties

This wraps up this post about class properties.  I’ll see you in the next post about this series about PowerShell classes where I will be discussing class methods.


Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

1 Comment
  1. Dave 2 years ago

    FYI, static properties can be modified at run-time. As in C# and other languages, static means there is only one copy of the variable, shared by the class and all its instances.

    Presuming $instance1 and $instance2 are instances of myClass, and staticProp is a static property of myClass:


Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2020


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account