PowerShell for the C# developer–Part 2

In the first part of the series we covered the development environment setup on how to get started with PowerShell. Now lets dive into some code. PowerShell can be used as a dynamic language. For a C# developer this can be one of the most frustrating points. In this post we will look at the following points:

  • Variables
  • If/else
  • Loops and Piping
  • Methods
  • File handling

So let’s get going Smile

Variables

When we look at a simple program of C# it might look something like this.

  using System;
   
  namespace ConsoleApplication
  {
  public class Program
  {
  public static void Main(string[] args)
  {
  string name = “Harvey Specter”;
  int number = 42;
  Console.WriteLine($”Hello {name}, your number is {number}”);
  }
  }
  }

view rawProgram.cs
hosted with ❤ by GitHub

Now in comparison here is the equivalent PowerShell code. Note that we do not need any Class or Method to get started. Simply start writing your script. Now variables are interesting under PowerShell. Lets add some more info to our PowerShell code to retrieve the type of the variables. The variable is assigned a type when a value is assigned. Since PowerShell is not compiled there are some potential pot holes a typical C# developer might into. For starters this is a totally valid statement. Resulting in the following output:

Variable is first of Type Int32 after assignment of a string has the type String.

We can be more strict in PowerShell by defining the type of the variable which will make the second assignment illegal. But this requires some additional effort on your end. If we would run the strict assignment we would be greeted by an error message which is more of what a C# developer would be used to.

And one more thing. Even though the variable $neverDefined never got defined. Well we can still access it’s value without an exception or error being raised. PowerShell output showing that the variable $neverDefined simply shows an empty string

Keep this in mind while developing since they might just come around and bight you in the foot later on.

Conditional Operators

When writing conditional code in C#, the standard choice is using if and else or for multiple options a switch/case. So a possible option would be to use them as follows: Apologizing to all the readers who have to work shifts Winking smile Lets look at how the same code would be implement in PowerShell: No huge changes or surprises So no surprises here. The major difference is the equality sign in the if check. Here is a small translation table of the equality signs you find in C# and PowerShell:

Purpose C# PowerShell
Equal == -eq
Not Equal != -ne
Greater Then > -gt
Less Then < -lt
Greater or Equal >= -ge
Less or Eual <= -le

Loops and Piping

There are many different constructs for looping in C#: for, while, do while and ForEach. So if we look at all the different types of loops in C#: In PowerShell the equivalent can be implemented like so: Now the ForEach loop is really great when having to iterate over a list of items. This is often what we end up doing e.g. “Iterating over a list of people to get the count by city” or a bit more PowerShelly “Iterate over number of host information to ensure that everything is okay and no action is needed”. While we could write that with the above for loop, there is a PowerShell called piping. Piping allows us to Take a collection and forward it to the next operation. We could rewrite the ForEach sample as follows: Pretty cool no? Smile We can even use the ForEach construct to loop over every item being forwarded: Note that $_ is always the current item we are going over in the ForEach Loop. The equal construct in C# is achieved with Extension Methods. Piping can be great to enhance readability since one can see the flow. But it also can make the code harder to debug, so be sure to keep the balance here.

Note: Even though foreach is an alias for ForEach-Object they behave differently. When using foreach the operation is paused until all elements are to be processed are present, in case of ForEach-Object it will process the items as they come in. This may lead to some unexpected side effects…

Summary

In this Blogpost we saw the basic programming structures in PowerShell compared to how they would be implemented in C#. Keep in mind that PowerShell is more dynamic and forgiving at runtime than C# which might lead to some unwanted side effects. In the next Post we will look at how we can implement Methods and work with Parameters which are not only handy for methods but also for Command Line Interface parameters.

References

There is more in this blog post series:

Updated: