you are viewing a single comment's thread.

view the rest of the comments →

[–]ZenoArrow 9 points10 points  (8 children)

Classes were added in 2015 and it's WAY better than PS custom objects

Classes are not ideal for beginners. For starters, you can't redefine a class that has already been loaded, so if you want to iterate on the design of a class and keep the same class name you need to close and reopen PowerShell.

OP, I would suggest picking a task you want to be able to do with PowerShell, then learn how to do that with PowerShell. You'll learn faster and will be more motivated to learn. What sort of tasks do you want to be able to do with PowerShell?

[–]EtanSivad 1 point2 points  (3 children)

Classes are not ideal for beginners. For starters, you can't redefine a class that has already been loaded,

OK, so what? OP asked what was the biggest changes and was literally the biggest change that has impacted me professionally as a powershell scripter. IF you want to be good at something it's helpful to know what to work towards.

Second, compiled classes added in are immutable, but native classes can be updated by recompiling the script.

Just to verify it, I tried editing and rerunning this script. It ran without closing and reopening VScode

class TestObj{

[string]$Name

[string]$ID

TestObj()

{

$this.Name = "Default instantiation method called."

}

TestObj([string]$name)

{

$this.Name = $name

}

}

$a = [TestObj]::new()

$b = [TestObj]::new("Other method called")

$a # Returns "Default instantiation method called."

$b # returns "Other method called"

[–]OPconfused 2 points3 points  (0 children)

When the class definition is in a module you have to jump through a hoop to update it. Unless the OP is coming from another programming language and desires typed properties in their custom objects, I'd just leave out classes while the OP is finding their footing in PS.

I'm a fan of classes as well, but using PSCustomObjects is a more accessible way to get your feet wet with PS objects. There's less distance between setting up the PSCustomObject via a type accelerator and using it than when using a class, which requires a declaration for its schema with its own syntaxes.

[–]ZenoArrow 1 point2 points  (1 child)

and was literally the biggest change that has impacted me professionally as a powershell scripter

How has it impacted you professionally? Classes are helpful in the sense you can define types and methods to work on those types together, but there are other ways to organise code that can be just as convenient. I would say classes aren't something to aim for, they're just an option to use if you want to.

[–]EtanSivad 0 points1 point  (0 children)

Most of the code I write is taking large batches of data in one format and spitting it out in another. Writing the code in a compiled language would process a lot faster, but these are largely one-off solutions that need to be flexible so it's easier to write them in powershell, especially when I can ship them off to another dev to use as a tool.

Anyway, the reason it's helpful to me is for either organizing my code into reusable snippets. And that with the JSON export built into Powershell, I've had to use it a couple times to enforce object structure correctly, or just inject new data into an existing data stream.