My presentation about PowerShell 5

Last week I did a presentation about "PowerShell 5 and OneGet" for my company. It was an introductory level session where I talked about some of the new features of PowerShell 5 and what is/how to use OneGet. It was a time boxed session set to last no more than thirty minutes, so I had to pick the topics I thought were the most relevant to developers. Small disclaimer, I did my presentation on the month of April of 2015, so keep in mind that some of the things I am going to talk about today, might be out of date by the time you read this post. Additionally, you may be wondering where is the PowerPoint for my presentation; well on my presentations I try to stay away from PowerPoints as much as I can, in a way to provide more demonstrations and engage everybody in a interactive session. On this blog post, I want to highlight the most relevant things I mentioned on my presentation.

Let's start with PowerShell, one of the best features included in version 5 is the support for classes and enums. It might not sound like a big deal, but at least for me, classes are far more "cleaner" and easy to manage than a swirl of functions. To be fair, before PowerShell 5 you could create a C# script with your class definition and instance it by calling New-Object, but this approach doesn't look very clean and can get quite challenging to maintain in no time, simply because you write your class in plain text and tell PowerShell to recognize it as a new type. Here's an example:

Defining class a before PowerShell 5:

$myClassScript = @" 
public class MyClass 
{ 
  private string _myName; 


  public MyClass(string myName)  
  { 
    _myName = myName; 
  } 


  public static int Add(int a, int b) 
  { 
    return (a + b); 
  } 


  public int Multiply(int a, int b) 
  { 
    return (a * b); 
  } 


  public string GetName() 
  { 
    return _myName; 
  } 
} 
"@ 


# add our custom Type 
Add-Type -TypeDefinition $myClassScript 


# call 'MyClass' static method 
[MyClass]::Add(4, 3) 


# create a new object of type 'MyCalss' 
$myClasstObject = New-Object MyClass "Jane Doe" 


# call method Multiple from a existing instance 
$myClasstObject.Multiply(5, 2) 
$myClasstObject.GetName()  

Defining a class in PowerShell 5:

Class MyClass 
{ 
  #Properties 
  [string]$_myName 


  #Constructor 
  MyClass([string]$myName)  
  { 
    $this._myName = $myName 
  } 


  #Methods 
  Static [int] Add([int]$a, [int]$b) 
  { 
    return $a + $b 
  } 


  [int] Multiply([int]$a, [int]$b) 
  { 
    return $a * $b 
  } 


  [string] GetName()  
  { 
    return $this._myName 
  } 
} 


[MyClass]::Add(5,9) 


$myClassObject = [MyClass]::New("Jane Doe") 
$myClassObject.Multiply(5,4) 
$myClassObject.GetName()  

Another awesome feature in PowerShell 5 is static code analysis (or script analysis). This feature was included in "Windows Management Framework 5.0 Preview February 2015" and it's still in experimental mode (download it here). With PowerShell repositories becoming larger and more complex to manage, there is a need to create coding standards and guidelines so all scripts are easier to interpret and manage across developers. The perfect scenario for code analysis usage is as a step on your development lifecycle, so on every commit to the main remote repository, the code analysis would run to validate the scripts.

The last feature I want to talk about is OneGet. This is not necessary tied to PowerShell 5 development, but since will be supported out-of-box in version 5, I thought it was worth mentioning. To Briefly explain it, imagine it as the evolution of NuGet. As you may know, NuGet is the main package manager used in Visual Studio. It's incredibly useful and easy to use. For example, do you want to include Entity Framework in project? Just right-click on the project name, click "Manage NuGet Packages...", search for it, install it and "bam" you're done, the package manager will download the required DLL's for your and execute any required scripts (defined by the package maker). Well, somebody thought, why not use this concept for applications? After all Linux has it for years (check apt-get) and that's how Chocolatey was born. Then the folks at Microsoft thought, "why don't we add support to a package manager for PowerShell modules?" Well, since the list was growing, the decided to create the OneGet project which will be the supporting platform for all of their package providers. Do you want to know more, check out their GitHub project.

I hope this information was somehow useful to you, or at least that got you interested in knowing more about PowerShell. Before I conclude this post, I would like to share some references, including the GitHub repository where the PowerShell scripts, used during the presentation, are hosted.

Whats new in PowerShell