PowerShell 101 for Messaging Administrator and IT Pros (Part 1)


If you would like to read the other parts in this article series please go to:

Introduction

I’m a big fan of discussion groups about Unified Communications, and I noticed that in most of the cases the IT Pro knows how to get stuff done, however, some have difficulties in creating scripts for large group of objects using PowerShell to facilitate the process.

So, the idea of this article series is to go over the basics of PowerShell and then see how we can create scripts to help us in our daily administration tasks on Exchange Servers while, you can use and apply the same concepts to different products that use PowerShell. As you may have noticed the list is getting bigger and bigger, from the top of my head we have Lync, DPM, System Center Virtual Machine Manager, System Center Operations Manager and bear in mind that Windows 7 and Windows Server 2008 R2 already have PowerShell built in.

One last piece of advice before starting this journey, I’m not a programmer, well I started my career programming in some old languages such as Pascal and Cobol, however, that is not my best suit and for that reason we are going over PowerShell from an IT Pro point of view. There are several ways to accomplish things so, if you find a way to do something better please, drop me a line in the MSExchange.org Blog and I will be glad to update the article or add the updates in the blog.

In this article and the next one we will be working on the requirements to build a certain level of scripts and after that we are going to explore a couple of scenarios and create some scripts from scratch. Then, we will tackle a couple of issues and use different techniques to get better results.

The basics and a little bit grammar

The first thing is how to get around and understand the commands within PowerShell even if you don’t have an Internet connection and you can do that easily using the PowerShell/Exchange Management Shell.

The first thing to understand is that all cmdlets are formed by a Verb + Noun and the most common verbs are: Get, Set, Remove, Add, Clear and so forth and the noun portion is based on the settings that we want to perform but for us exchange guys a few common nouns are: Mailbox, MailEnable, Contact DatabaseAvailabilityGroup (by the way we are going to see how to reduce this name using an alias :)).

Taking advantage of Autocomplete and help features…

One of the best features of PowerShell is the autocomplete functionality, which facilitates life for the administrator during a shell session. Basically, at any time you can hit the tab button and the text auto-completes for you. Let’s say that you just remember the option database in order to list the databases, then you can type in Get-Mailbox and start hitting the tab button, all options after Mailbox will be shown (database is the 5th or 6th option ahead) however, the tab feature does not stop there, you can also use it for parameters. Let’s say that you finally found the cmdlet that you were looking for and now you want to list the databases of a specific server but you don’t remember the full syntax, so you can type in – (dash) and start hitting the tab again and all options will be displayed. If you know that it starts with an “S” then just type S and hit the tab button and all parameters starting with “S” will be displayed. Not so bad, isn’t it?

Now that we know how to search commands we can use the built-in help of PowerShell. There are a lot of ways to do that and the first one is to use man *string* where the output will be all cmdlets with that string. In Figure 01, we are getting information about all cmdlets for our users with contact string, and we have a nice list with all cmdlets including a synopsis that gives you a good idea what to do with that cmdlet.


Figure 01

If you need further details about the cmdlet, then you can run man <cmdlet> -detailed which will display information including: parameters, examples, remarks, description, syntax and synopsis. If you want to cut to the chase and just see a couple of examples then run man <cmdlet> -examples, or if you are alone on a Saturday night, and you have nothing to do, then I would suggest running man <cmdlet> -full!

Cheating and finding out the cmdlets required, using Exchange Management Console

If for some reason you have no idea about the command you need to use, you can always cheat and use Exchange Management Console to find out what cmdlet you are looking for. Basically, any change that you do in the console will enable an icon on the lower left corner, and clicking it will have the cmdlet used to process the change that we have defined (Figure 02). This helps the administrator who wants to create a script but may not remember how to define a certain property or even the cmdlet. In Figure 02, we opened properties of the user and checked the Hidden From Address List on the General tab, the PowerShell button will be available and the result will be the cmdlet code to be used to perform such change when we hit either Apply or OK.


Figure 02

Building your first “Hello world!” script

If you have already done that, you can skip this section, but if this is your first time congratulations and I’m glad to help you on your first “Hello world!” script. It has been a while since I wrote mine but I still remember it when I was learning my first language (Pascal). I had several classes and readings to understand variables, procedures, vectors, functions, and so forth before going to the computer lab to test it (Yeap, it was a long time ago when computers were not that popular in my hometown). At that time as a learner I wanted to see some action, and the coolest moment ever was when I was able to write something and get results on the screen using some information entered by the user.

In order to define the output text of a script you can run Write-Host “String”, and if you want to show a warning message (the ones in yellow and black background) you can use Write-Warning “String”, while, you can also use Write-Error, you got the idea, right?

So, in order to build your first script open the PowerShell or Exchange Management Shell, type in notepad myfristscript.ps1 and in the first line type Write-Host “Hello World”, save it and then run .\MyFirstScript.ps1 (remember that you can also use tab to autocomplete) and here we go we have the text defined in our script, as shown in Figure 03.


Figure 03

Bear in mind that you may need to run Set-ExecutionPolicy Unrestricted in case you get an error about signed scripts when running your script.

So far, we know that by just saving a simple notepad with a .ps1 extension we can start building scripts. We have enough base to create a simple set of tasks in a script, we can also add some text before and after with the write-host as we have just seen and voilà a useful script. A good example is the report of mailbox sizes in an organization Exchange server, where we could just save that long line in a ps1 file and you don’t need to find that on the Internet every time you need it.

Conclusion

In this initial article we went through the basics of PowerShell and important fundaments of the language.

If you would like to read the other parts in this article series please go to:

 

    Leave a Comment

    Your email address will not be published.

    This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    Scroll to Top