Playing with Classes in PowerShell v5 Preview

One of the features we’re working on in the latest version of PowerShell is the ability to define custom classes.

If you’ve ever used a PSCustomObject or a Hashtable in a script for storing related data, you’ll probably find classes to be a useful addition to your scripting toolkit.

 

001
002
003
$point = @{ X = 0; Y = 0 }
$point.X = 10
$point.Y = 20

 

While PSCustomObjects and Hashtables let you group related data, what if you want to perform actions on that data? For example:

001
002
$point = @{ X = 0; Y = 0 }
$point.Move(10, 20)

 

This is the kind of scenario that classes really excel at. You use classes for holding “buckets” of related data, and operations on that date.

While the release notes in the V5 Preview (and of course, upcoming actual documentation) give an overview of how to define a class in PowerShell, here’s what they end up looking like:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
class Point
{
    ## Define two properties, using PowerShell's regular
    ## variable syntax. You can limit variables to a type
    ## using regular type contraints, toos:
    ## [type] $VarName = initialValue
    $X = 0
    $Y = 0

    ## Define a method (that returns void), and takes
    ## two parameters. You can limit the types on these
    ## parameters, of course.
    [void] Move($xOffset, $yOffset)
    {
        $X += $xOffset
        $Y += $yOffset
    }
}

## Create a point and use it
$point = [Point]::new()
$point.Move(10, 20)
$point

 

And in use:

PS C:\temp> c:\temp\point.ps1 | Format-Table -Auto

 X  Y
 -  -
10 20

                                                                                                                        

Now, if you wanted to extend this concept far further than you would ever want to, you might get this 🙂

 

saddle

And Show-BoxDemo in all of its glory. TAB switches between objects, and there are a few other hotkeys in the source.

http://www.leeholmes.com/projects/boxdemo/Show-BoxDemo.ps1

And if you’re curious:

iex(iwr http://www.leeholmes.com/projects/boxdemo/Show-BoxDemo.ps1)

5 Responses to “Playing with Classes in PowerShell v5 Preview”

  1. asdf writes:

    Have you guys considered adding primary constructor syntax, e.g.:

    class Point([double]$x, [double]$y, [double]$z) {}

    and/or naming the constructor “new” so we don’t have to keep repeating the class name over, e.g.:

    class Point {
    new($xCoord, $yCoord) {
    $X = $xCoord
    $Y = $yCoord
    }

    [double]$X
    [double]$Y
    }

  2. PowerShell 5.0定义类 - PowerShell 中文博客 writes:

    […] 声明:本文示例代码和sh’g图片来自Lee Holmes的博文Playing with Classes in PowerShell v5 Preview。 […]

  3. Fabien writes:

    Hi 🙂
    Unable to find type [System.Collections.Generic.List]. Make sure that the assembly that contains this type is loaded.

    Any idea, i use 2012 R2 + Posh V5 november preview

  4. Nicholas Getchell writes:

    I think I see invoke expression here.

  5. How to create a PowerShell 5 class - 4sysops writes:

    […] management needs, the other great advantage of PowerShell classes is that they allow you to store “buckets” of data that can actually perform actions on their own. You also have excellent code reuse because you can […]

Leave a Reply