Archives for the Month of October, 2014

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)