When I thought about what I considered the most important, fundamental thing to learn about PowerShell I went through a couple of options; Piping? The help system and Get-Command? -WhatIf?
All useful things to learn but it has to be how everything in PowerShell is an object. It’s how you can build Really Cool Stuff from lots of simple bits and it’s also the thing that catches me out occasionally.
Which works, as you’d expect; it writes “Hello World” directly to the host (your screen).
It looks about the same, right? So what’s the difference?
($Hello is a variable; use them to store objects. You refer to variables with the “$” prefix)
So don’t worry about outputting things, let PowerShell do it for you.
Once you’ve got an object there’s all sorts of things we can do with it too.
You can see what you can do to a particular object by piping it to Get-Member;
$Hello | Get-Member
(The “|” is the pipe symbol. You can use it to join multiple cmdlets together to do cool stuff)
Each object will have a Type to it. “Hello World!” was a string of characters (or string for short). String objects have the properties and methods (information you can get or stuff you can do) as above. But objects of a different type will give you different options;
No point having a ToUpper() for numbers but ToDecimal() might be more useful!
This is why the recommendation is to keep things as objects for as long as possible and not to write things to the screen as strings; when they’re objects you get Type-specific commands you can use. Once it’s written to a string (to the screen for example) you’re restricted to only the string commands.