Thursday, November 12, 2015

OO Hello World - PowerShell



The Hello World in PowerShell is finally here!

"Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework." Its been around for ~8 years (2006), but it is until now, with the release of version 5.0, that they finally introduce classes as a built-in feature of the language.

Now that the language have classes, properties, methods, constructors, static members, inheritance and so, we can compare its syntax with all other languages on this blog.

By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html where I give some details on WHY these "OO Hello World series" samples.

Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.
class Greet {
    [String] $name;
    Greet([String] $name) {
        $this.name = ([String]$name[0]).ToUpper() + $name.Substring(1);
    }
    [void] Salute() {
        Write-Host ("Hello {0}!`n" -f $this.name)
    }    
}

$g = [Greet]::new("World")
$g.Salute()

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.
class Greet {
    [String] $name;
    Greet([String] $name) {
        $this.name = ([String]$name[0]).ToUpper() + $name.Substring(1);
    }
    [void] Salute() {
        Write-Host ("Hello {0}!`n" -f $this.name)
    }    
}

$g = [Greet]::new("World")
$g.Salute()



Both version are completely similar.

The Program Output:









PowerShell Info:
“PowerShell is an automation platform and scripting language for Windows and Windows Server that allows you to simplify the management of your systems. Unlike other text-based shells, PowerShell harnesses the power of the .NET Framework, providing rich objects and a massive set of built-in functionality for taking control of your Windows environments. ” Taken from: https://msdn.microsoft.com/en-us/mt173057.aspx

Appeared:
2006
Current Version:
Developed by:
Microsoft
Creator:
Jeffrey Snover, Bruce Payette, James Truher
Influenced by:
C# and Perl, Tcl, Cmd
Predecessor Language
~WSH (VBScript, JScript)
Predecessor Appeared
1998
Predecessor Creator
Microsoft
Runtime Target:
CLR 4.x
Latest Framework Target:
CLR 4.x
Mono Target:
No
Allows Unmanaged Code:

Source Code Extension:
".ps1", "ps*"
Keywords:
35
Case Sensitive:
No
Free Version Available:
Yes
Open Source:
No
Standard:
No
Latest IDE Support:
PowerShell ISE
Language Reference:
More Info:


No comments:

Post a Comment