Welcome to installment #2. In case you missed installment #1 - Getting Started – here’s the link. With the Getting Started blog, you tried a few basic Windows PowerShell cmdlets. I hope you took a bit of time to explore what other cmdlets are available in Windows. But now it’s time to get Configuration Manager hooked up to your PowerShell environment. Let’s start with the easy method.

Loading PowerShell from the Configuration Manager Console

Start by launching the Configuration Manager console. In the upper left corner, there’s a blue rectangle. Click the white arrow in the blue rectangle, and choose “Connect via Windows PowerShell”.

 

It’ll take a moment to load. Once it’s loaded, you’ll see a prompt that contains your site code. My site code is T16, so mine looks like:

PS T16:>

Let’s just verify everything is working fine. The first cmdlet you’ll try is Get-CMSite. This cmdlet will return information about the site you’re connected with. Give it a try!

PS T16:> Get-CMSite

You should see information about your site.

Importing the Configuration Manager PowerShell Module

Great, now that you have your site information, let’s trying loading the Configuration Manager module manually. Hit your Windows Key and type “PowerShell” – then right-click PowerShell (x86) and choose “Run as administrator”. You should now see your PowerShell environment.

Windows PowerShell
Copyright © 2012 Microsoft Corporation. All rights reserved.

PS C:\Users\Administrator>

There’s a built-in PowerShell cmdlet to import new modules called Import-Module. To import the Configuration Manager module, you will have to specify the path to the Configuration Manager Module. Just like in a Command Prompt, by using theCDcommand in PowerShell, we can change folders on our drive. But, I’ll give you a trick here to make your typing easier. We’ll use the TAB key to help us out.

Type CD c:\pro (press TAB repeatedly until you get) CD ‘C:\Program Files (x86)’.

Press the ‘\’ key, then type mic and press TAB repeatedly until you get CD ‘C:\Program Files (x86)\Microsoft Configuration Manager’.

Press the ‘\’ key, then type adm and press TAB until you getCD ‘C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole’

Press the ‘\’ key, then type b and press TAB until you get CD ‘C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin’

Press Enter to change to this folder.

Type import-module c then press TAB to have it auto-complete the module name. You should now see:

PS C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin> import-module .\ConfigurationManager.psd1

Add –verbose to the end of your command and press ENTER.

PowerShell lists for you all of the cmdlets as it imports them. You did it! Now, all the ConfigMgr cmdlets are available to you. Just to confirm, let’s try the Get-CMSite cmdlet.

PS C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin> Get-CMSite

Oops! PowerShell didn’t seem to like that. That’s because we were pointing to the hard drive (our Configuration Manager Console path) not the Configuration Manager site. Let’s change over to the site. You can use the CD command to change to your site. In my case, my site code is T16, so I type CD T16:

PS C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin>CD T16:

PS T16:\>Get-CMSite

This should return the site information – the same as you saw earlier. So, you’re all set!

Help!

Before we get too far, it’s a great idea to get the help file up to date. PowerShell 3.0 includes a new feature to update your help over the internet. If your computer is connected to the internet, you can type:

PS T16:\>update-help –module configurationmanager

Notice that I added a new parameter to the cmdlet. Most every cmdlet will take parameters. We’ll spend more time in next week’s blog covering parameters, but in this case, the parameter limits the cmdlet to just update the Configuration Manager’s help.

One of the basic PowerShell cmdlets that you will use regularly is Get-Help. Try it out – here’s how to get help about the Get-CMSite cmdlet:

PS T16:\>get-help get-cmsite

Exploring the Configuration Manager Cmdlets

Now, let’s try a few cmdlets to retrieve information from your site. Since you should have a management point, try this:

PS T16:\>Get-CMManagementPoint

You should have a distribution point too, so try this:

PS T16:\>Get-CMDistributionPoint

In fact, there are quite a few cmdlets available to manage your site systems. I like being able to list out cmdlets – it helps me explore, or figure out how I might complete a task in PowerShell. So, let’s get a list of the cmdlets that have to do with your management point. We’ll use some filter parameters with the Get-Command cmdlet to retrieve the cmdlets from our Configuration Manager module that have a name that contains “managementpoint.” Here’s how:

PS T16:\>Get-Command –module ConfigurationManager –noun *managementpoint*

You should see five cmdlets listed. You notice in our command line we have some parameters. We limited the cmdlets to our configuration manager module and we found only cmdlets that had “managementpoint” in the noun. Let’s see all the “Get” cmdlets for our site system roles.

PS T16:\>Get-Command –module ConfigurationManager –verb get –noun *point*

That’s quite a few. I don’t really want to count all those one by one, but I’m curious to know how many objects there are. In fact, as you’re working with PowerShell, you will often need to know the number of items in an object. So, let’s take this opportunity to do just that! Since I know you were paying close attention during blog #1 – you recall we could store the results of a cmdlet in a variable. So, let’s store the whole list of site system “get” cmdlets in a variable, and then ask PowerShell to tell us how many there are. Just declare a variable, and assign it to our previous command. To save on typing, press your Up arrow key, then press Home, and type $a=  then press ENTER to run the command. Then, on your next line, just tell PowerShell to output the count for $a.

PS T16:\>$a=Get-Command –module ConfigurationManager –verb get –noun *point*
PS T16:\>$a.count
20
PS T16:\>

You now know a couple of basic cmdlets that will help you explore and learn -get-help and get-command. I encourage you to try these cmdlets to find out what things you can do with PowerShell. Some of the cmdlets are very powerful and can delete or remove lots of information – and quickly! Best to stick with the “Get” cmdlets for now.

As we close out installment #2 – I’ll leave you with one more command to try. This will list all the cmdlets, and sort and group them alphabetically by noun, one page at a time. Press the “space bar” to view the next page.

PS T16:\>get-command -type cmdlet -module configurationmanager | sort-object -property noun | format-table -GroupBy noun | more

Note that there is a new concept introduced here – the “|” (pipe) symbol. When you use this – PowerShell simply sends the output from one action to the next action. One of the more powerful cmdlets is format-table. We’ll use it from time to time. It has a lot of cool functionality – and you can even use the format-table cmdlet to limit the information returned by a previous cmdlet. Ok. Ok. You’ve twisted my arm; one more cmdlet to try. We’ll put together three things you’ve learned - the ConfigMgr cmdlet, the pipe symbol, and the format-table cmdlet.

PS T16:\>Get-CMPackage | format-table “Description”

Now we’re getting somewhere! In the next blog, I’ll show you how to get more information from your sites and packages, and how to change site settings and package settings.

As always, to learn more, jump over to the PowerShell User’s Guide here: http://technet.microsoft.com/en-us/library/cc196356.aspx.

--Dave Randall

This posting is provided "AS IS" with no warranties and confers no rights.