Power-What…?

Lets get started with some basics:

From cmd.exe to powershell.exe – Some background

Most of you will know the comand line interface tool cmd.exe, which has been published in all Windows NT operating systems and is similar to the Unix-Shell. With the cmd.exe the user can start some programms like ipcofig, telnet or others. Morever there are certain commands which can for example alter and display the filesystem (dir, mkdir).

Maybe the biggest feature is the possibilty to use Pipes just as in the Unix-Shell. Pipes (“|”) give you the ability to pass on the return values of one process to another process.

Example: dir C:\Windows | more 

dir C:\Windows | more
dir C:\Windows | more

The first part of the command-line lists all files which are located in C:\Windows. All the files are passed on to the more-Command, which limits the output to just one cmd-prompt site at one time. To be exaclty for eyery return value of the dir-command (one file or folder in C:\Windows), the command after the pipe gehts called with the return value of the first comand as an input value.

“Is new always better?” – Or why should we use PowerShell

So, the cmd-Shell seems to be pretty cool and you can do lots of stuff with it, even write some .bat-scripts (google it ;-)). But there is a major problem, which makes it complicated to script huge tasks. The shell is textdriven (Strings, Strings everywhere). What does this mean? Every single command returns a formatted String as return value. This is pretty OK, for a human reader in front of the monitor, but its pretty lame for a script, which should behave differently, if at least one file in a directory is greater than 1 MB.

Of course this is possible with the help of String parsing, regular expressions and so on, but one has to know the format of the String in all details and has to hope that this will never change, not a single character. There has to be a better approach to store informations in a handy way.

Why don´t we try to reproduce the reality that we would like to process into the shell – as identical as possible. This paradigm is called object orientation and was first mentioned after the software crisis in the 1980s and the following evolution of software devolopment. This approach helps to structure the data and the PowerShell scripting language uses exactly this. Every object in the reality has a counterpart, which can be abstracted to only fit its needs, in the shell/programm. Such an object is a capsule for the attributes of the concrete object (The Object File has the attributes, Name, Filesize, Path) as well as the methods, the action of an object (The Object File has the methods Delete, Create…). You can see the advantage of this approach in the following two pictures. Results of an command in PowerShell are always an object or in other words structured data with a certain behaviour, which you can address at any time.

Attributes of a File-Object
Attributes of a File-Object
Methods of a File-Object
Methods of a File-Object

What can we do with PowerShell?

Whereas the old cmd-Shell is based on a textdriven scripting language, the new powershell scripting language is object-oriented. Next to it all other features of the shell, like Pipelines, are available within in the PowerShell. So now we have a object-oriented scripting language with pipelines, but what can we do with it?

Within in the Windows Universe there are three major ways to gather and manipulate relevant data and write software.

  • .NET-Framework (software framework)
  • WMI-/CIM-Classes (gather ALL information about the Computer and accessory parts with the Common Information Model)
  • COM-Objects (e.g. handle Microsoft Excel)

PowerShell is kind of a layer above those threes. From PowerShell you can access all sub-areas and there are even a few hundred pre-defined functions (so called cmldets), which encapsulate the complex logic and accesses to the .NET-Framework, WMI-classes and COM-objects.

To put it in a nutshell, PowerShell is a object-oriented scripting language, whose aim it is to offer an easy API to complex tasks via predefined cmdlets and on the other hand to be the ONE tool, which can perform every task in the Windows Universe.

So lets learn to use the Power of Shell!

Look forward to the upcoming posts.

Best wishes

Advertisements

3 thoughts on “Power-What…?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s