PowerShell for N00bs 1: Hello, World

"Hello, World!"

 That's how you write "Hello, World" in PowerShell.  Give it a try:

 PSH> "Hello, World"
"Hello, World!"

PowerShell has some very unorthodox behaviours, such as "To execute a string, print it."  (There's more to it than simply "print it," but that's for later.)  Actually, if it's told to execute any chunk of data (a highly technical term, one we'll revist later), it will print it.  If it's something that isn't a string, but PowerShell has some rule on how to change it to a string, then it does so.  Otherwise, it prints the data's type, as if you were creating the data in a C# program.

Actually, "To execute a string, print it" is a simplification.  As we saw, PowerShell will happily print any chunk of data, be it a string, a number, an array or some esoteric type.  So is it "To execute a chunk of data, print it?"

 Even that's a simplification.  See, PowerShell is Object Oriented.  That means the string "Hello, World!" isn't just raw data - it is an 'object' - a computational abstraction that encapsulates data, properties of that data, and functionality automatically created when the object is created.  And how did we create the object? By typing "Hello, World!"  The PowerShell interpreter saw the command, created a string object, executed it, and then disposed of it.

 So, "To execute a chunk of data, print it" is actually "To execute an object, print it."  But an object can have many types of functionality (called methods) besides just outputting data.  Well, PowerShell calls one by default: ToString(). 

 "Take this string and turn it into a string" sounds rather redundant, doesn't it?  Yes, but remember, the rule has become "To execute an object (not just a string), print it."  Every object has a ToString() method.  This is how a numeric 1 gets translated into the character '1'.  The two are indeed different - the 'data value' for the character '1' is actually 49.  Look it up in an ASCII table.  (If you don't have one handy, look for one in Bing.)

To review, we learned two things:

  • "To execute an object, call its ToString() method, then print the value returned."  This is useful because we know every object has a ToString() method, so we can safely print any chunk of data, er, object.
  • PowerShell, like an onion, has layers.