Powershell: A Scripting Language for .NET Junkies and Administrators
Introduction
Have you ever tried using the wrong tool for the wrong job? Using a butterknife in place of a flat-head screwdriver? Hitting Caps Lock every time you want to capitalize just one letter? Using VIM to write a program when you could be doing it in Nano…just kidding. Well, let me tell you of a problem I once had using a great shell but for the wrong OS. As many of you know, I started my development career by learning Python and the Linux CLI in hopes that I could be transferred from Desktop Support to Application Development for a former employer. I found many of the built in tools in BASH so useful, that I would sometimes go as far as downloading Cygwin so that I could use them in a Windows environment. And that right there was a problem… I knew Windows had a tool, called Powershell, that was built to serve as a command-line utility and a means to automate daily tasks, but I kept using BASH because it was what I was familiar with. After having issues getting Cygwin to work properly on my current workstation and learning of a project involving cleanup of a Windows 2016 file-server, I finally broke and started learning it. I knew that I would have no issues figuring out how to use it, but I did not expect that I would love using it more than BASH. Before I go any further, I want you to know that this post is NOT about claiming Powershell is better than BASH. BASH was designed and works best for Unix/Unix-like systems, and Powershell was designed and works best for Windows (though you can use it on Linux and Mac thanks to .NET Core). This post is merely an explanation of Powershell and why I love using it.Verb! That’s What’s Happening!
Most of the commands in shell scripting languages somewhat resemble what the do. Using BASH as a reference, ls is for list, cp is for copy, mv is for move, ect. But there are other commands that are not so descriptive with their names. Can you get “search for string” out of grep? What about “find text and replace with” from awk? One of the most notable features of Powershell is that it does away with this guesswork by using a strict Noun-Verb syntax. Need to search for all occurrences of the word “test” in all text files contained in a specific directory? Simple, type: Select-string “test” C:\your\path\here\*.txt

Different Language, Same Framework
.NET developers who need a quick script can take joy in the fact that Powershell is written in their favorite framework! If you are well-versed in C# or VB.NET, you will have no trouble picking up on Powershell. The same methods you use in your .NET programs will be accessible to you in Powershell. Additionally, all your favorite .NET DLLs can easily be attached to your script so you can take advantage of your favorite libraries.A Scripting Language with Class
Like Python, Powershell has the ability to utilize classes and objects in its scripts. As we all know, an object is a collection of related attributes and actions. Let’s use a .NET DateTime object as an example. Below, you can see I am saving the current datetime in a DateTime object called $dt.




Automate Your Workflow
While it can be a powerful tool for developers, Powershell was built primarily for Windows sysadmins to help automate operations, such as setting up new users in Active Directory or creating Sharepoint sites in Office 365. As stated earlier, I am currently using it to help clear space in one of our file servers. Instead of manually seeking out files older than a certain age, I have a script that will seek them out and email a report to the directory owner with a list of files that are subject to be purged. Another script related to this project will move old files to an Archive folder, which can later be deleted from our server. These scripts help us save time and decrease the chance of human error being responsible for purging the wrong files.Dynamic Scoping
One unusual aspect of Powershell that sets it apart from other languages is its ability to take advantage of dynamic scoping. This means that any variable you make in a script is accessible anywhere. Take a look at the program below for an example.
