You might be a bit confused why I’m writing about PowerShell in the same breath as Java, C#, and Visual Basic.

  • Java
  • C#
  • PowerShell
  • Visual Basic

In the traditional day-to-day corporate setting, you would find Systems Administrators on one team and Software Developers on another. Typically the developers would know very little about how the operating system works, and the administrator would know very little about how the application works. In recent years, there has been a trend, called DevOps, which combines the two – dare I say it – paradigms, and unites workers. It also, of course, creates synergy.

PowerShell, if anything, has brought developers and system administrators together. In a vocation where previously very little coding was done, an administrator now has full access to a .NET wrapper language and its APIs… even if the administrator doesn’t realize it yet. In its truest form, PowerShell is essentially a programming language disguised as a scripting language. It’s included as a free download with Microsoft Windows.

What’s the difference between scripting and programming?

Let’s say you manage an Active Directory environment and want to get a list of all users in the “Retirees” Group. You could go through the Active Directory User Interface and manually write down (or copy) every user name in the group. But what if you also wanted information about each user, such as Home Directory Path and Phone Number (or other AD attributes, such as “favorite drink”)?

This is where PowerShell comes into play. With a simple script, you can get a list of all the Retirees, and their phone numbers and favorite drinks. Then you can have a heck of a reunion.

How does this apply to programming?

In the past, most administrators didn’t do much scripting beyond batch files. PowerShell has brought a new level of accessibility to those who otherwise would not be interested in writing full-blown programs. Because there is this uniting in progress, I thought it would be wise to show a PowerShell example alongside all the programming examples.

How do you use PowerShell?

You write some code into a .ps1 file and run it from a PowerShell command prompt.

That simple?

Yep.

What’s a Hello World app look like in PowerShell?

Well, technically it’d be a “Hello World Script”. Here’s the code:

And here’s what it looks like when run:
helloworld-ps

No class declaration? No public static void main? No curly braces??

Remember “Spaghetti Code”?

PowerShell, being predominantly a scripting language, is very spaghetti-code-friendly. You technically do not need a single method or class declaration to run a PowerShell script. This is in contrast with fully-blown programs, where you not only need class and method declarations for the entry point, but (due to the scope) typically several others as well.

In the long run, it is possible for PowerShell to be used to write programs with Graphical User Interfaces and mouse-driven environments. But while Java is good for backend programs and C# is good for event-driven programs, PowerShell’s niche is really all about scripting.

Share

Comments are closed.

Post Navigation