Archive for the ‘Classes in PowerShell 5.0’ Category


Inheritance in PowerShell Classes

Wednesday, March 16th, 2016 by June Blender
If you're learning about classes in Windows PowerShell 5.0, one of the first new concepts that you'll encounter is inheritance. When you create a class that is based on another class, your new subclass or child class automatically gets the inheritable members (e.g. properties and methods) of the parent class or base class. Inheritance is a really powerful and useful concept, so it's important that you understand it. Fortunately, it's pretty easy. Also, the inheritance principles that you learn in PowerShell are also used in other programming languages, so learning them in PowerShell gives you a head start on new…   More »

Creating Objects in Windows PowerShell

Monday, October 26th, 2015 by June Blender
Applies to: PowerShell 5.0.10240 I was recently in the beautiful city of Copenhagen, Denmark, where I was the guest of PSUG.DK, PowerShell User Group in Denmark. We met in the swanky Microsoft offices for a hands-on about classes in Windows PowerShell 5.0. Microsoft Offices, Copenhagen, Denmark. In the hands-on lab, we create a class, create instances of the class, and add constructors, properties and methods. Then, we try some of the more advanced features, like static properties and methods, inheritance, and interfaces. To keep the class in a reasonably brief, when we create objects, I show New-Object and the New…   More »

Why Do We Need Constructors?

Wednesday, October 21st, 2015 by June Blender
While traveling after PowerShell Summit Europe in Stockholm, I was honored to be the guest of the Microsoft Technical User Group in Oslo, Norway (@MTUG_Norge). This well-established user group meets on the beautiful campus of the University of Oslo, a short light-rail ride from downtown Oslo. We got together for a hands-on lab on classes in PowerShell 5.0. It was an experienced group -- mainly IT operations folks -- who are well-versed in PowerShell. The concepts are all new, but the group of 35-40 people were quick to understand them. One thoughtful guy asked a particularly good question: Why do…   More »

Hidden … sort of: The Hidden Keyword in PowerShell Classes

Wednesday, March 18th, 2015 by June Blender
The Hidden keyword hides properties and methods (and other members, like events) in a PowerShell class. When a member is hidden, you can still view and change it, but the Get-Member cmdlet doesn't display it without the Force parameter and the hidden members are not available to auto-completion features, such as PrimalSense and IntelliSense. You can apply the Hidden keyword to any member (property, method, event, etc), including static members. It is not case sensitive and it can appear anywhere in the member signature before the member name. As a best practice, I suggest placing it first to simplify troubleshooting.…   More »

Enumerated Types in Windows PowerShell 5.0

Monday, January 5th, 2015 by June Blender
Follow @juneb_get_help [Update: The original post referred to "enums" as "enumerators" when, in fact, they are "enumerated types." By contrast, enumerators allow you to process items one at a time. Many thanks to Windows PowerShell MVP Dave Wyatt (@msh_Dave) for the correction.] Enums have always been a part of Windows PowerShell, because they're an important part of the .NET Framework. But they're about to become even more popular, because changes in Windows PowerShell 5.0 make them very easy to create. ----------- Enumerated types ("enums") define a set of values, knows as "members" of the enum. You use enums to create a set…   More »

Find variables with class

Wednesday, December 24th, 2014 by June Blender
Follow @juneb_get_help I've been playing with the Windows PowerShell 5.0 preview and especially with its new class feature. After I create my class, I often create several instances of the class and experiment with them in my session. For example, I created a Wine class and then created several instances of my wine class. I saved each Wine instance in a variable. $PSWine = [Wine]::new("PSCabernet") $Shiraz = [Wine]::new() ... After playing with the instances for a while (and enjoying a few sips of PSWine), I couldn't remember all of the Wine objects that I'd created in my session. They were…   More »

Beyond custom objects: Create a .NET class

Tuesday, December 2nd, 2014 by June Blender
In yesterday's blog post, I used the Select-String cmdlet to capture the table of keywords and references from the about_language_keywords help topic and then used the new ConvertFrom-String cmdlet in the Windows PowerShell 5.0 preview to convert each row of the table into a custom object (PSCustomObject). The custom object worked quite well. I could search, sort, format, and use the objects in Windows PowerShell. PS C:\> $k = $table | ConvertFrom-String -TemplateContent $template | Select-Object -Property Keyword, Reference PS C:\> $k | where Keyword -eq "Trap" Keyword Reference ------- --------- Trap about_Trap, about_Break, about_Try_Catch_Finally But, it's just a simple step from the generic…   More »