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
VariablesPermalink
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}"); | |
} | |
} | |
} |
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.
$name = "Harvey Specter" | |
$aNumber = 42 | |
echo "Hello $name, your number is $aNumber" |
$myValue = 12 | |
$myValue.GetType() | |
$myValue = "Hello" | |
$myValue.GetType() |
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.
[int]$i = 12 | |
$i.GetType() | |
$i = "Hello" | |
$i.GetType() |
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.
$name = "Harvey Specter" | |
$aNumber = 42 | |
echo "Hello $name, your number is $aNumber and this guy never was here $neverDefined" |

Keep this in mind while developing since they might just come around and bight you in the foot later on.
Conditional OperatorsPermalink
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:
using System; | |
using System.Text; | |
namespace ConsoleApplication | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
StringBuilder messageOfTheDay = new StringBuilder(); | |
var currentDate = DateTime.Now; | |
if(currentDate.Day % 2 != 0) | |
{ | |
messageOfTheDay.Append("Today is an odd Day, "); | |
} | |
else | |
{ | |
messageOfTheDay.Append("Today all should be even, "); | |
} | |
switch(currentDate.DayOfWeek) | |
{ | |
case DayOfWeek.Monday: | |
case DayOfWeek.Tuesday: | |
case DayOfWeek.Wednesday: | |
case DayOfWeek.Thursday: | |
case DayOfWeek.Friday: | |
messageOfTheDay.Append("that being said you should be working..."); | |
break; | |
case DayOfWeek.Saturday: | |
case DayOfWeek.Sunday: | |
messageOfTheDay.Append("yay it's the Weekend!!!"); | |
break; | |
default: | |
throw new InvalidOperationException("This program only knows the 7 weekdays...."); | |
} | |
Console.WriteLine(messageOfTheDay.ToString()); | |
} | |
} | |
} |

$currentDate = Get-Date | |
$messageOfTheDay = "" | |
if($currentDate.Day % 2 -ne 0) | |
{ | |
$messageOfTheDay = "Today is an odd Day, " | |
} | |
else | |
{ | |
$messageOfTheDay = "Today all should be even, " | |
} | |
switch (($currentDate.DayOfWeek)) | |
{ | |
"Monday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." } | |
"Tuesday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." } | |
"Wednesday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." } | |
"Thursday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." } | |
"Friday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." } | |
"Saturday" { $messageOfTheDay = $messageOfTheDay + "yay it's the Weekend!!!" } | |
"Sunday" { $messageOfTheDay = $messageOfTheDay + "yay it's the Weekend!!!" } | |
default { $messageOfTheDay = $messageOfTheDay + "This script only knows the 7 weekdays...." } | |
} | |
echo $messageOfTheDay |
Purpose | C# | PowerShell |
---|---|---|
Equal | == | -eq |
Not Equal | != | -ne |
Greater Then | > | -gt |
Less Then | < | -lt |
Greater or Equal | >= | -ge |
Less or Eual | <= | -le |
Loops and PipingPermalink
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#:
using System; | |
namespace ConsoleApplication | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
while (new Random().Next(0, 50) > 50) | |
{ | |
Console.WriteLine("While... Random seems to be bellow 50."); | |
} | |
do | |
{ | |
Console.WriteLine("Do While... Random seems to be bellow 50."); | |
} | |
while (new Random().Next(0, 50) > 50); | |
for (int i = 0; i < 10; i++) | |
{ | |
Console.WriteLine($"Current index is {i}"); | |
} | |
var items = new[] { "Donna", "Harvey", "Rahel", "Mike" }; | |
foreach (var item in items) | |
{ | |
Console.WriteLine($"Hello {item}"); | |
} | |
} | |
} | |
} |
while((Get-Random -Minimum 1 -Maximum 100) -gt 50) | |
{ | |
Write-Host "While... Random seems to be bellow 50." | |
} | |
Do | |
{ | |
Write-Host "Do While... Random seems to be bellow 50." | |
} while((Get-Random -Minimum 1 -Maximum 100) -gt 50) | |
for($i=1; $i -le 10; $i++) | |
{ | |
Write-Host "Current index is $i" | |
} | |
$items = @("Donna", "Harvey", "Rahel", "Mike") | |
foreach ($item in $items) | |
{ | |
Write-Host "Hello $item" | |
} |
$items | Write-Host |

$items | foreach { Write-Host "$_ has a length of" $_.Length } |
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…
SummaryPermalink
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.
ReferencesPermalink
There is more in this blog post series: