I'm Not A Developer

Are you intimidated by scripting? Does PowerShell seem too much like programming to you? You aren't a developer, why should you learn this mumbo jumbo?

It turns out, PowerShell is quite easy to get started with. Can you run ipconfig? Do you know how to give someone instructions? You could probably pick up the PowerShell basics in a month of lunches or so.

Don't believe me? I spent a few minutes to compare a simple task in four languages. PowerShell is a single, easy to understand command. Python is two lines and starts to include some syntax like .().

We won't even look at the C example here, but check out this C# code that simply reads and prints out the content of a file:

// Modified with suggestions from Anton.
// Better ways to do this, but, I'm not a developer ; )
using System;
using System.IO;

class ReadFromFile
{
    static void Main()
    {
        foreach(string s in File.ReadAllLines(@"C:\file.txt"))
        {
            Console.WriteLine(s);
        }
    }
}

You can get a feel for what's going on, and there are many ways to skin a cat, but let's compare this to PowerShell:

Get-Content C:\file.txt

This was one example among many. Read or write a CSV, execute a SQL query, create a VM, kick off an AzureRM template, modify an AD user, the list goes on. All of these are individual PowerShell commands, that handle a whole bunch of code behind the scenes, giving you simple, task-based commands.

This lets you worry about the actual problem you are trying to solve, not the nitty gritty programming details. Have you ever had to write your own sorting code, rather than just using Sort-Object ?

Come join the fun, you'll save yourself time and effort, and help out your team and organization in the process. Learn PowerShell.

Cheers!

About the Author

Warren Frame

Systems Engineer with a penchant for PowerShell, science, cooking, information security, family, cookies, and the Oxford comma.

5 Comments

  1. Well, I hope no one will use your C# example for anything.

    Why do you even need counter variable?

    Why aren't you using using statement to dispose file stream?

    Why you imported System namespace, but not System.IO?

    Right C# will be one-liner:
    Console.WriteLine( File.ReadAllText(@"c:\file.txt") )

    • Hi Anton!

      Agreed, many ways to skin a cat! In this case, the example comes from Microsoft (linked in the longer post this referenced).

      The intention was to reflect similar behavior - reading a file iteratively, rather than all in one shot. Didn't highlight this, because it's not really related to the takeaway.

      I'm assuming that you aren't positing that C# is more abstracted / task based than PowerShell? The takeaway is that PowerShell is easier to use (in part) because it is more abstracted / task based than other languages.

      Lastly, I assume a C# developer will look for C# references, over a short blog post from a PowerShell user : )

      Cheers!

    • Hey Warren,

      Thanks for reply!

      I've just posted to point that C# example is awful. As for PowerShell — it is great both for simple and big complex tasks, but I think it solves different types of tasks rather than C#.

      That code snippet is really outdated (it even says "Visual Studio .NET 2003") and it looks so bad nowadays as someone made it up just to show that C# is awful and feeble while it is not 🙂

      P.S. - to get file as array of lines in C# one can use File.ReadAllLines(@"C:\file.txt").

      • Hi again!

        Wow, I glanced at it quickly and assumed it would be reasonable, given that it was the first google result, and from Microsoft. Should have realized... What on earth do they have a counter in there for (like you said)? Yikes.

        Anyhow! Any tips on the bare minimum to read through line by line? I know in the C# world you would never write something like this, but could the following be simplified? (apologies for the butchered formatting)


        using System;
        using System.IO;

        class ReadFromFile
        {
        static void Main()
        {
        foreach(string s in File.ReadAllLines(@"C:\file.txt"))
        {
        Console.WriteLine(s);
        }
        }
        }

        Cheers!

    • If you want to spit file line-by-line and only then output it to console your latter example it totally OK.
      Assuming that user would add extra operation per line later.