Everybody is talking about PowerShell and how cool it is in conjunction with several Microsoft Products. As you probably already know, PowerShell is an interactive, commando-based scripting environment that simplifies the administrative tasks for system administrators and developers.
With PowerShell you are able to communicate and to automate several Microsoft Products such as Exchange Server, SQL Server, SharePoint Server and even third party tools such as VMWare. If you want to automate SharePoint Server 2010, you have to learn PowerShell! The stsadm.exe command will be replaced soon or later by PowerShell completely. Because of that, I’m going to start a new post series talking about PowerShell basics.
Walkthrough
This post will be subdivided into following sections:
- What is PowerShell?
- What are the requirements?
- A small demo explaining some simple information retrieval commands
What is PowerShell?
As already mentioned before, PowerShell is an interactive, commando-based scripting environment delivered with the latest Windows operating environments. It can be used to automate tasks easily because you are able to…
- … work with .NET, COM, WMI, XML and more
- … supports transactions (if one command fails, all other commands fail too and are rolled back)
- … execute some scripts remotely
- … use it in your .NET applications and extend them
- … extend it with your own commands
- … you can run “old” command prompt command (e.g, dir)
- … you can run external programs (e.g., notepad)
What are the requirements?
Currently, there are two different PowerShell versions available. The version 1.0 and 2.0. You can find PowerShell 1.0 in Vista SP1 and Windows Server 2008. You have to install the .NET Framework 2.0 if you want to use PowerShell.
PowerShell 2.0 can be installed as addon in the operating systems listed above or is directly delivered with Windows 7 and Windows Server 2008 R2. You must install the .NET Framework 2.0. However, there are some functions that use the .NET Framework 3.5. Therefore, it is recommended to install it on your machine.
A small demo
Basically, you work with commandlets (cmdlets), which are .NET classes, and run them in your PowerShell environment. You can combine their results by using the so called object-pipelining and construct in this way complex automation tasks.
First of all, you need to start the PowerShell console. You should see something similar in the next figure:
Note: You can start PowerShell either by pressing the
icon, running it from the start menu or by entering the command “PowerShell” in the console.
Now, let us start using our first cmdlet by entering get-childitem and pressing enter. You get the directory listing of the current directory. You should see something like this:
You can add parameters to your cmdlet and filter the directory listing. Type get-chilitem *.txt. You should get all *.txt files of your current directory.
Another interesting variation of the get-childitem is the get-childitem –recurse. This command returns recursively all contents of this and all subdirectory starting from the current directory. You might see something like this.
Now, you are probably asking if it is possible to get all items of a given extension of the current directory and all subdirectory. Yes we can! Type get-childitem –filter *.txt –recurse and you get an answer like this:
Probably, you might already have noticed that the functions listed above are similar as the old command prompt command dir. In fact you can also use the “old” command by typing it in PowerShell. Be warned, this dir has nothing to do with the old command of the old command prompt. This is only an alias of the command get-childitem to simplify the learning phase of Windows administrators and developers.
Note: In our first example you are getting only a list of something. Nothing special, you might say. But you will see how impressive it will become when we will combine such commands with the object-pipelining functionality of PowerShell.
Finally, I conclude this post by listing some commands that I find helpful to begin with. Play around with these commands to feel comfortable with the new environment. In the next postings I will tell more about the PowerShell functionalities and features:
- get-commands: returns all commands (tip: use it with filtering parameters).
- get-help [commandLetName]: returns the help of the specified commandlet (use get-help [commandLetName] –full to get a full help description).
- get-date: returns the current date
- get-process: returns all running processes of the machine
- get-service: returns all Windows Services of the machine. It becomes an interesting command when you combine it with parameters functions. (e.g., get-service *sql* returns all services with SQL in their name, i.e. SQL Server services).
- stop-service –name [ServiceName] -verbose: stops a service with the passed name
- start-service –name [ServiceName] -verbose: starts a service with the passed name
- get-hotfix: returns all installed hotfixes and the corresponding knowledgebase number of this machine
Summary
In this post I explained what PowerShell is and what are the requirements to start with PowerShell. Afterwards, I made a small demonstration of some commands and listed useful commands to start with.
Hope this helps,
Patrick