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.

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:

$NewEmployee = [employee]::new()
$NewEmployee.FirstName = "Stephane"

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:

$NewEmployee.FirstName

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.

Class Employee {
     [ValidatePattern("^[a-z]+$")]
     [string]$FirstName
     [ValidatePattern("^[a-z]+$")]
     [string]$LastName
     hidden [string]$UserName
     [ValidateSet('Internal','External')]
     [string]
     $EmployeeType
     [ValidatePattern("^OU=")]
     [string]$OU
     hidden static [string]$DomainName = "DC=District,DC=Local"
}

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.

$employee = [Employee]::new()
$employe | get-member -MemberType Properties
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.

$employee | Get-Member -MemberType Properties -Force
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.

[employee]::DomainName
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:

$NewEmployee = [employee]::new()
$NewEmployee.FirstName = "Jeffrey"
$NewEmployee.LastName = "Snover"
$NewEmployee.EmployeeType = "Internal"
$NewEmployee.OU = "OU=Internal,OU=Users,OU=HQ," + [employee]::DomainName
$NewEmployee
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.

avatar
2 Comments
  1. Dave 4 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:

    ([myClass]::staticProp -eq $instance1::staticProp)
    # True
    [myClass]::staticProp = 5
    $instance2::staticProp
    # 5
    ($instance2::staticProp -eq $instance1::staticProp)
    # True
    avatar
  2. Jay 2 months ago

    You have defined $DomainName as both hidden and static:

    `hidden static [string]$DomainName = “DC=District,DC=Local”`

    But when you create an instance of the class in your final example, you do:

    `$NewEmployee.OU = “OU=Internal,OU=Users,OU=HQ,” + [employee]::DomainName`

    Shouldn’t the addition of + [employee]::DomainName throw an error since it’s hidden? I’m trying to understand the hidden attribute and how it differs from common public/private method declarations in other languages.

    So, to get this straight, even though a property is hidden, it can still be used/queried from outside of the class?

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2022

CONTACT US

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

Sending

Log in with your credentials

or    

Forgot your details?

Create Account