Windows PowerShell: an introduction

Posted on Updated on

To use and/or control an operating system one or more interfaces are needed. Such an interface provides a working environment wherein users and administrators can do their thing; in a way it can be considered as a “working place” or some kind of “home”. That’s why they are mostly called shells. Some shells are graphical, some are text based and others are some sort of a mix between them (most of the time those are text based with some basic graphical window/tool bar/status bar/menu/… around them). Windows contains 2 default shells, but third party shells can be installed too. Users and administrators almost always use the default shells though and there are different reasons for this, some of them mentioned here:
* most people don’t even know other shells exist; heck, most users don’t even know what a shell is!
* using another shell, especially a graphical one, gives the impression another OS is used. Most people don’t have a good feeling about that.
* other shells have a higher chance to contain more bugs and less features: it’s not made or tested by Microsoft.
* people are afraid they won’t really get to know “the real (default) Windows (feeling)”. This seems a stupid reason at first, but actually it isn’t! Suppose you are reading a book explaining a lot of practice information about Windows. I guess reading such a book while using another shell must be hell! Almost everything and everyone supposes you are using the default shells.

The 2 shells which are present in Windows by default are very well known:
* Explorer: a graphical shell, which is loaded after login by default. Everything you see after login happens in this shell. The shell gives you the ability to do a lot of things, including exploring whatever you want; hence the name “Explorer”. IMHO this name is misleading: “Explorer” can refer to, well, anything… It just doesn’t name the shell unequivocally. Something like “Graphical Windows Shell” (GWS) is perhaps not the most sexy name, but it says everything! And if some exotic name is preferred anyway, well, then Explorer doesn’t seem right to me at all! In Windows it also confuses people with the “Windows Explorer” (for exploring the file system) or “Internet Explorer” (IE) (for web surfing). (Yes, I know, Windows Explorer is actually a part of Explorer, but at a high level we could see them as seperate components).
* Command line: a text based shell, also called a command line shell because you have to type “lines of commands”. Command line is often abbreviated as CMD. To be consistent with my Graphical Windows Shell name, I would have called Command line “Textual Windows Shell” (TWS) or “Command line Windows Shell” (CWS), but hey, I’m a boring guy, right? No, to be honest, the name does have one advantage: “Command line” is a name that is not confusing and almost everyone understands, without using “unknown” terms like “shell” for example. Anyway, Command line can be started from within Explorer (uhum, that’s right: a shell running within another shell, why not?), but there are also other situations where this shell can be met (for instance, if you boot your Windows XP system and start the Recovery Console).

We are not going to talk about Explorer anymore, but will focus on the default command line shell offered by Windows. This shell is present since the ultimate beginning and it has always felt like the good old MS-DOS (don’t forget that MS-DOS was the OS from Microsoft used before Windows and was even still needed to run earlier versions of Windows for many years!). Ok, since the end of MS-DOS one can say that the look has been changed a little bit and much more features were added too of course. But despite of this, weren’t you all feeling that Command line looked old, ugly and boring? And Command line not too limited? Was this the default command line shell in a modern Windows?

I guess Microsoft had the same thoughts, because they decided it was time to create a successor. You could easily call it Command line 2 if you want, because there are so many improvements, but at the same time it seems quite compatible to the classic Command line. To stress the fact that the new version was very powerful, they called it Windows PowerShell (PS).

Windows PowerShell is available in Windows by default since Windows Vista, but it can be downloaded and installed on Windows XP and Windows Server 2003 too. It’s also part of application software; actually the first release of Windows PowerShell was in fact part of Exchange Server 2007! Why? Because PS was really, really, really needed to manage Exchange Server 2007. This fact on its own already stresses the importance of Windows PowerShell.

Ok, PS is part of Windows Vista, Windows Server 2008 and Windows 7 by default. Then why the hell is the old Command line still present? Well, PS is indeed the successor of Command line, but don’t forget that not everything is compatible. You should see the presence of Command line next to PS in Windows Vista and later as a smooth transition to a PowerShell only environment. This way nobody is really brutally forced towards Windows PowerShell; suppose some disaster happens and you need to type a few commands which don’t run very well in Windows PowerShell, you don’t know the alternatives for PS and don’t have the time to look it up and test it… Don’t forget though that you do need to prepare for Windows PowerShell, because sooner or later Command line (the classic version) will be gone…

The first version of PS was 1.0, but now we are already using 2.0. Things go fast in IT World, aren’t they? You can find PS in the %windir%\system32\WindowsPowerShell\v1.0 folder. PowerShell can be started in a few ways by default:
* double click the powershell.exe file from Windows Explorer
* right click the powershell.exe file in Windows Explorer and select “Open”
* from CMD, PS or the Run box (Start – Run) type “%windir%\system32\WindowsPowerShell\v1.0\powershell.exe”, the same but with the windir variable replaced by its value or just “powershell.exe”
* choose Windows PowerShell in Start – All programs – Accessories – Windows PowerShell. This is actually a shortcut to the executable, completed with a few arguments, which makes it not the “super default” way of running PS
* in the Search box (Start) type “Windows PowerShell” or “PowerShell”. Note that when you install the tool Scriptomatic “PowerShell” will run Scriptomatic and not PS.

When Windows PowerShell starts a window pops up and provides you with the PS interface. We see the following text appearing within the window:
* “Windows PowerShell”: well, that’s obvious, right? It’s just the name of the program/shell.
* “Copyright (C) 2009 Microsoft Corporation. All rights reserved.” (the year can be different): it basically says PS is subject to copyrights from Microsoft.
* a white line to seperate the first 2 lines a little bit from the part where the actual work is done. The previous two lines can be considered as some kind of a title or header.
* “PS C:\Users\User1> “: this is the prompt. The first part refers to Windows PowerShell, telling us we are in Windows PowerShell of course. Then we have a space, followed by a path to some folder (more about this later) and then we have the greater than sign (“>”) to tell us this is the end of the prompt. Well, actually there is also another space, after the > character. After this prompt the user/administrator can type… yup, commands 🙂 That’s why the prompt is also called the command prompt. The word “prompt” does have a meaning: it “prompts” you to type something or indicates where and when you can type something.

Very important to understand is that a decent command line shell gives the opportunity to run a batch file with commands. A batch file is a file which contains commands “in batch” and can be run by a command line shell. You have probably seen a lot of .bat and .cmd files, well, that’s what we’re talking about. “bat” refers to “batch”, “cmd” refers to “CMD” (the Command line). The Command line can run such files IF they actually contain compatible commands.
If the commands are not just a simple sequence of commands anymore, but more or less look like the source code of a programming project, well, then we speak of scripting. In this case the batch files are scripts. Command line supports scripts; and because Command line always looked like MS-DOS (concerning the features AND the look) this type of scripting is also called “DOS scripting”, to distinguish it from other scripting languages like VBScript. The fact that Command line supports scripting means there is a scripting language related to it.

It’s very important to realize that DOS scripting was a simple scripting form and higher scirpting languages were still needed for the serious scripting work (VBScript was THE de facto default language for this). With the coming of PowerShell this has all changed: interactive use, simple scripting and serious scripting can now all be done by the same system. Only in certain circumstances there could still be a need to use something else, but let’s not talk about specialties too much right now.

Of course Windows PowerShell also supports scripting, but starting from PS we try to avoid terms like Command line, CMD and (MS-)DOS. The language used in PS is just, well, the same name actually: Windows PowerShell (PS). If you use classic DOS commands/scripting that is compatible with PS, you may use the bat, cmd or ps1 extension for the script to be able to run it in PS. The first two will also run in CMD, but not the third one. If you use “PS only” commands/scripting, then you do need the ps1 extension (“ps” obviously standing for PowerShell) for your script, which will only run in PS. If you use classic DOS commands/scripting that is not compatible with PS, then bat or cmd are the only options for the script extension and of course it will only run (decently) under CMD. In normal situations you should now focus on PS commands/scripting and running under PS, so as a rule of thumb this means you should use the ps1 extension for all your new scripts. Do notice that I said “in normal situations”: exceptions can still exist, especially if PS is not installed on all your systems (but perhaps you should begin to upgrade your old Windows systems and/or install PS first then).

You can wonder why batch files/scripts are useful, especially in PowerShell, where one command line can already perform a lot. Well, let’s give you a few arguments:
* recurring tasks: if you have to execute something more than once, it’s easier and faster to save the set of commands and execute that saved set again later, especially when the set of commands is quite big and complex and when it has to be executed quite a lot.
* documenting and sharing: by saving the set of commands you can easily document it and share it with other people. You can save the set in a documentation system, a Word file, a PDF file,… but in the script form it’s also easy for yourself and others to run it quickly.
* automating: when the system has to execute your set of commands automatically (for example periodically) and thus not interactively, then you have no other choice than save it in a script and schedule that script.

It’s the intention of Microsoft to make PS as compatible as it can be with older CMD commands. That’s why many command names from CMD still exist in PS, with of course the same purposes. For example, in CMD you could use the command “dir” to list the contents of a folder. Well, in PS you can still use the same command. But… there are limits to this comptability and consistency. In PS “dir” will result in a slightly different output than in CMD, although the core results (telling you which folders and files reside under the specified folder) still are the same of course. If the PS output is not what you needed for some reason and you can’t customize it to what you have in mind, than you could still use CMD. For example, it could be that your IT manager needs a list of files and folders in a certain directory and requires a very fixed output style for all the listing within the same year. In this case you could still use CMD for the dir command for the rest of the year and prepare for the new PS style starting from next year. It’s a silly example, but I suppose it’s an obvious one without using difficult commands you perhaps don’t understand.

Last but certainly not least, I should stress the fact that PowerShell is not just made for control, management, administration and maintenance of Windows. It can also be used to address application software like Exchange Server and Active Directory (AD). The list of software the can be managed this way will grow exponentially, making PowerShell a solution virtually addressing your whole environment.

We can summarize by repeating that Windows PowerShell (PS) is the big successor of Command line (while trying to be as compatible as possible) and is THE command line shell of the future, for interactive use as well as for simple and serious scripting. This means it also “replaces” VBScript and variants in practice. PowerShell not only gives you the oppertunity to manage, maintain and administer Windows, but also a lot of application software, like Active Directory and Exchange Server. PowerShell is one system you should use for everything except real programming (but then we are talking about “real” developing of course). It is accepted very well by the Windows community and can already be marked as cool, future ready and unmissable. Learn PS and you can do A LOT of stuff, now and in the future. PS cannot be ignored if you want to be a real Windows system administrator or engineer.

Hopefully I can write down another article with the very first practical steps in a few days or weeks. Stay tuned!



Leave a Reply

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

You are commenting using your 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