Let the Power of Shell be with you! A Powershell review

Today we live in the era of graphical user interfaces. Click here, click there – everything is done by users’ clicks. If you are a developer you are probably familiar with the drag&drop programming style – drag a control from a box, drop on the form and double click to create a handler. However, it seems the fashion is changing again. MVC isn’t working in a drag and drop way, even WPF is handled better without this technique. And, what about the operating system itself?

If you were using any LINUX/UNIX distribution before, you probably know the ‘black’ command prompt where you navigated through the system with the use of commands. Windows command prompt, known from DOS and its batch files aren’t so handy and useful as LINUX bash – but have you tried PowerShell?

The power of command prompt

Yoda

Today, end-users just click an icon representing an application and that’s it! When you want to personalize the application you need to go to the configuration screens and set some options. However, to do something special or automatize the process you still need to use the black window with a prompt – the command prompt!

Many users don’t know what it actually is or are afraid of the word, simply because it tells them that some problems appeared. Nowadays, all users using the command prompt are known as geeks (even developers, but is it possible for a geek to be even more geekish?). Nevertheless, some years ago a command prompt was in common use.

PowerShell, a tool for whom?

I won’t explain how to use PowerShell, but instead I would like to share some advantages of PowerShell from my daily experience. Most importantly, the shell can be used not only by administrators but it’s also suitable for developers and even average users. Now, let’s have a closer look at the numerous advantages of PowerShell!

Runs on .NET

Yes, it runs on .Net and thanks to this you have direct access to the .NET framework libraries. Just like the .NET framework, Powershell ships with Windows 7, so there is no need to install the tools separately. Additionally.

For example in .NET you have access to the static property of DateTime object. In PowerShell it will look like this:

[datetime]::now

Notice that PowerShell, unlike C#, is case-insensitive. 

Fully Objective

Any output from the command (so called cmdlet) is an object. By choosing the dir command you won’t get a text list, but you will get a list of objects representing a file type object information. To get the information just pipe the result to Get-Member cmdlet  which will consequently print the object information with all available properties.

Dir | get-member

If you run some exe application, like ipconfig, inside PowerShell  the output will be a string type. In the scripts you can create your custom object dynamically, add properties and pipe it to another script.

function Get-Files {
  ls | %{
    New-Object -TypeName PSObject |
      Add-Member -NotePropertyName File -NotePropertyValue $_.Name -PassThru |
      Add-Member -NotePropertyName Extension -NotePropertyValue $_.Extension -PassThru
    }
}

C-based script syntax

If you know the C syntax (Java, PHP, C#, C++) you will quickly learn how to write scripts. You can write code directly in the command prompt and later put it into function, then load it into memory and execute just the function.

function Get-TextFiles {
  foreach ($file in $(dir -filter '*.txt')) { Write-Host $file }
} 

Feel the power of Tab

If you don’t remember what kind of parameters a command has you can write the ‘-‘ character and after that press tab. In this way you can grab the parameter options from the command. The best thing is that it also works for your PowerShell functions! If you need any explanation you can get some help for the cmdlet by looking through the manual.

Get-Help FunctionName

Easy navigation

You can navigate through the registry or environment variables like in case of the files on a hard drive. Also by installing some external module you can work in the same way with IIS application pools. As an example, you can browse environment variables using

ls env:

See, you are even able to create your custom drive! To get all available drivers just write:

Get-PsDrive

Linux users’ friendly

Yep, that’s true. Many commands from Linux are aliased to PowerShell cmdlets – like ls, cat,ps, kill, even man which is aliased to Get-Help (dir is also an alias). Still the user will feel the lack of such applications as wget but that’s not a problem – there are plenty of PowerShell scripts created by the community, which allow to fill that hole.

Many tools inside

You have the ability to load and save CSV files, XML files and also Json files (since version 3), run background jobs and that’s all for free. It’s very helpful during development and analysis to create or update some files. There are a lot more tools and functions available, but unfortunately a separate blog should be created to describe them all.

Integrated Scripting Environment

GUI ISE

You can write scripts in a notepad, but it is very helpful to use some ‘IDE’ which will color the syntax and also guide you through scripting. A so called ISE is available since PS2  – it’s a graphical environment which helps you to easily debug the script by putting break points inside the script. It’s also possible to set break points from command line but it’s not so handy. The latest, third version, brings a bit of a fresh air and some modifications which make scripting effortless. However, if you want to have more power then you can install PowerGui which has also a good IntelliSence editor.

Do I use PowerShell?

I used Linux some years ago when I was still studying, and the thing which took my breath away was bash. Returning to Windows I missed the lack of controlling the system through a command prompt. Fortunately, those days are gone or should I better say the bright days are back. I have played with Powershell since the very first version appeared. At the beginning I used it only to check how it looks inside and how to write some custom modules, but now I am automatizing the build and publishing tasks in my project. The interesting fact is that within Windows 8, Powershell is available directly from Explorer’s ribbon, next to our old command prompt. 

MarcinIsLazy

Day by day the amount of work automated by PowerShell in the project is growing. I’m using it to generate a change-log from SVN, increase the build number, generate and build packages, zip files, deploy on test servers, generate SQL scripts, and also for opening my most often used web pages. For sure it won’t think and write code for me, but the magic command PowerShell takes the humdrum, back-water work from me – isn’t that cool?

Try it for yourself! You can look at the official PowerShell blog or check its impressive community. Have you used PowerShell already? What are your impressions? For what purposes are you using PowerShell? Please share!

Tags:

6 comments

  1. You need to look at a proper shell like bash or zsh to realise just how limited powershell is.

    I moved away from .NET/windows after discovering what a real shell should do.

    Powershell would need a lifetime to catch up and be open sourced.

    1. Please elaborate. What can you not do with PowerShell (which gives you tight integration with .NET, object-based pipeline, ubiquitous tab completion, hostable by pretty much any process, etc) that you can do with the (string-based pipeline) shells you mention? Are you confusing PowerShell with the standard (and sadly utterly primitive) Windows console that it usually runs in on a vanilla Windows installation?

    2. I agree with Niklas and also with you. Powershell is quite different than bash – it is hard to make a valuable comparison, because in some cases object-based pipeline is really useful and some not. I do not see any reason why PS should be open sourced – with help of API (which is described and documented very well) anyone can write his own tools – the same is with any other framework like .NET or Java. I remember also that after PS was released, some month later bash had also a parameter prompting feature. I need a shell to make some automation and PS meets my demands.

Comments are closed.