Essential PowerShell: Learn how to find what you are looking for

October 25, 2007

As silly as this may sound, learning how to find what you’re looking for in PowerShell is key if you want to learn PowerShell quickly and easily.  PowerShell is a rather unique language in that most of the information you will need to use the language is retrievable by using the language itself.  The problem for newcomers to PowerShell who like to dive right in when learning something and who don’t want to read through lots of documentation to get started is that they need to know some PowerShell to learn more about PowerShell and what it can do.

When I first started learning PowerShell I was fortunate to be able to see PowerShell presented twice, both times by Jeffrey Snover, the “father” of PowerShell.  In both presentations as he wrapped things up he mentioned that if there’s one thing you should make sure that you take with you about PowerShell it’s a set of five cmdlets that are key to learning more about PowerShell.  They are (in no particular order): Get-Help, Get-Command, Get-Member, Get-PSDrive and Get-Alias.

Rather than pulling the documentation about these cmdlets into this article, I thought it would be more useful to follow the KISS principle and list a series of questions you might ask yourself about PowerShell accompanied with the PowerShell script (mostly one-liners) that can be used to answer those very questions primarily using the cmdlets I mentioned above.  Here are the questions and their answers to help get you started.

I’ve heard PowerShell cmdlets all follow a verb-noun syntax.  What verbs are available in PowerShell?

Get-Command -CommandType Cmdlet | Group-Object -Property Verb -NoElement

What cmdlets use the verb “out”?

Get-Command -Verb Out

What cmdlets use the noun “command”?

Get-Command -Noun Command

What examples are available for the Tee-Object cmdlet?

Get-Help Tee-Object -examples

What is the full help information for Tee-Object?

Get-Help Tee-Object -Full | Out-Host -Paging
# Note: I could have replaced “Out-Host -Paging” with “more” and the result would have been the same

What are the non-common parameters of Set-Location (with their documentation)?

Get-Help Set-Location -Parameter *

What cmdlets have a parameter like “path”?

Get-Help * -Parameter *path* | Sort-Object -Property Name

What general PowerShell help topics are available?

Get-Help -Category HelpFile,Provider

I need general help on operators.  Where can I find that information?

Get-Help -Category HelpFile,Provider | Where-Object { (Get-Help $_.Name | Out-String).Contains(“operator”) }

Now that I see the help topics and providers that reference the keyword “operator”, how do I see the details?

Get-Help about_operator
Get-Help Variable
# etc.

What aliases are defined for the ForEach-Object cmdlet?

Get-Alias | Where-Object {$_.Definition -eq “ForEach-Object”}

I’ve heard PowerShell even treats the registry as a drive.  What other drives are there?

Get-PSDrive

How do I start browsing the Registry in PowerShell?

Set-Location HKLM: # or cd HKLM:
Get-ChildItem # or dir
Set-Location Software # or cd Software
# etc.

I noticed that there is a drive for environment variables.  How do I use that to check the value of my Path environment variable?

(Get-Item env:Path).Value

How do I add all of my installed PowerShell snapins to the current session so that I can use their cmdlets too?

Get-PSSnapin -Registered | ForEach-Object { if ((Get-PSSnapin $_.Name -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin $_.Name } }

(Note: More useful one-liners using Get-PSSnapin can be found here.)

I need to enumerate a bunch of services and then do other things.  Since these are enumerated as objects, how can I tell what I can do with them (i.e. how can I tell what properties and methods they have)?

Get-Service | Get-Member

Are there other basic questions that you think should be on this list?  After learning how to find the information you need within PowerShell, what has been your biggest stumbling block?  Let me know in the comments!

Kirk out.

Technorati Tags: , , , ,


PowerShell presentation in Ottawa on November 22

October 19, 2007

It’s official!  I’ll be presenting PowerShell at the upcoming Ottawa Windows Server User Group meeting on November 22, 2007.  I’m quite excited about this because I don’t think there have been any local PowerShell presentations yet, so hopefully there will be a good turnout for this event!  Here are the details for the event (copied from the OWSUG site):

November 22nd 2007 User Group Meeting

Topic:

Becoming a Poshoholic: 12 steps to develop a healthy obsession for Windows PowerShell

Description:

Windows PowerShell in its inaugural version is already a tool that should be included in the toolbelt of any Windows IT professional or Windows software developer. It’s a command-line shell, an interactive scripting environment and a scripting language like you’ve never seen before all in one. It’s also the future of Windows server administration. If you don’t know Windows PowerShell yet, now’s the time to get started. Join Kirk Munro, a well known Poshoholic, as he guides you through 12 steps that will help you get on the PowerShell bandwagon more quickly while developing a healthy obsession for Windows PowerShell.

About the speaker:

Kirk Munro is a Senior Software Developer for Quest Software. He’s spent the last four years working on Windows Management products at Quest, specializing in data retrieval from Active Directory and Exchange data stores. Recently he became the world’s first self-proclaimed Poshoholic and now he is redirecting his career so that he can focus at work on this newfound obsession that has been taking so much of his time outside of the office.

Location:

Microsoft Canada Co.
100 Queen Street Suite 500
Ottawa, Ontario
K1P 1J9

Agenda:

5:00 p.m. Event registration
5:30 p.m. Presentation
7:30 p.m. Q & A
8:00 p.m. Door Prizes

Notes: Pizza and Pop will be served, Please RSVP to help us order enough of both.

RSVP: https://www.clicktoattend.com/invitation.aspx?code=122277

I hope to see you at the event!

Kirk out.

Technorati Tags: , ,


It’s a great time to be a Poshoholic!

October 14, 2007

What a fun time to be working with PowerShell!

Just this weekend I got my hands on the beta of PowerShell Plus and I started looking at the new PowerShell Community site that soft-launches tomorrow.  Last Monday PowerGUI 1.0.11 was released with the fantastic PowerGUI Script Editor.  On Friday version 1.0.5 of the Quest AD Cmdlets was released.  The Virtual PowerShell User Group hosted their inaugural meeting earlier this month (if you missed this you can get the recorded video here).  Local user groups are starting to talk about PowerShell and spread the good word.  PowerShell Analyzer 1.0 is about a week away.  Next month Jeffrey Snover will be presenting new features in PowerShell 2.0 at TechEd IT Forum 2007 in Barcelona (I hope there will be a webcast for that session!).  VMWare is providing a technology preview of their ESX server snapin to testers soon.  The library of PowerShell books that are available keeps growing.  More and more Microsoft and third-party products are releasing with their own PowerShell snapins.  Add to that PowerGadgets, PowerShell Community Extensions, the PowerScripting Podcast, several active community sites, a busy newsgroup, lots of webcasts, many other PowerShell projects on CodePlex and likely a bunch of things I’m not able to think of right now and you can’t help but feel that it really is a great time to be a Poshoholic!  Are you on the train yet?

Kirk out.

Technorati Tags: , , , ,


PowerGUI 1.0.11 with the PowerGUI Script Editor is now available

October 9, 2007

Following up the teaser post that I published this past weekend, I’m happy to tell you that PowerGUI 1.0.11 was officially released yesterday.  This is a truly great product, it’s free, and it is a must have for any serious user of PowerShell.  If you’re still not convinced that you should be using this product, take a look at Dmitry Sotnikov’s post about “Notepad for PowerShell”.  It includes a screenshot of the fantastic PowerGUI Script Editor application that was included as part of this release plus other details.

Enjoy!

Kirk out.

Technorati Tags: , , , ,


PowerShell CTP coming before end of year with WinRM support

October 9, 2007

As Hal Rottenberg pointed out earlier, some interesting news has surfaced about the next version of PowerShell (2.0?).  According to an article about WinRM, there is a CTP of PowerShell coming before the end of the year and this CTP will support running cmdlets against remote computers using WinRM as the remote transport protocol.  My bet is that this will be released at the TechEd IT Forum 2007 conference in November.  Here’s the snippet from the article:

Something else to consider. Sometime before the end of the year an upgraded CTP (Community Technology Preview) version of Windows PowerShell will be released, an upgrade that will enable you to run most Windows PowerShell cmdlets against remote computers. The catch? This new version of Windows PowerShell also relies on WinRM as its remote transport protocol. If you’re interested in using Windows PowerShell to manage remote computers you’ll need to download and install WinRM on your Windows XP and Windows Server 2003 machines.

If you haven’t gotten up to speed on WinRM already, now’s the time to start!

Kirk out.

Technorati Tags: , , ,


PowerGUI 1.0.11 release is just around the corner!

October 6, 2007

According to Dmitry Sotnikov’s most recent blog post, the next release of PowerGUI (version 1.0.11) is slated for next week.  It may even be available by the time you read this post!  I have had the great pleasure of beta testing PowerGUI 1.0.11 for the past week or so, and this is another fantastic release of this truly great product.

As with previous upgrades to PowerGUI it only takes a few minutes with the new version to see that the PowerGUI team has been very busy enhancing this product.  I hadn’t even finished the installation of the new version when I became totally distracted after noticing one of the enhancements in this version, the PowerGUI Script Editor.  This feature is great news for the PowerShell Community!  Finally a PowerShell script editor with great features that facilitate writing scripts even when you don’t know the syntax by heart, and a free one to boot!  Working with PowerShell just got much, much easier.

Add to that the performance improvements to the startup time for PowerGUI and all of the bug fixes, and you’ve got yourself another great release from the PowerGUI product team!  For a full list of enhancements and bug fixes, check the PowerGUI Feature Map page.

For those of you who haven’t tried PowerGUI yet, this would be a great release to start with!  It’s free and it adds a ton of value to working with PowerShell so there’s really no reason not to try it if you’re even remotely interested in PowerShell and wondering what all the fuss is about.  Plus there is a very active PowerGUI community where you can post questions, notify the developers about possible defects, watch webcasts to see how it works, and raise enhancement requests.  The PowerGUI team actively monitors and participates in the community, so if you need help with anything or want to start a discussion about using PowerShell with PowerGUI, about PowerGUI itself or about the Quest AD cmdlets, this is the right place to go!  I’m a regular on the community forums and will likely try to respond to your questions if someone from the PowerGUI or Quest AD cmdlets teams doesn’t get back to you first.

Kudos to the PowerGUI team for the fantastic products they’re producing!  I can’t wait to see what they’ll do next!

Kirk out.

Technorati Tags: , , , , , ,


PowerShell Challenge: Processing arguments in a function or script

October 6, 2007

Recently I was experimenting with passing arguments into a function or a script.  I wanted to see how variable length argument functions or scripts could be created.  Here’s what I had set up in my test script:

param([string[]]$args)
$args

I then saved this script as test.ps1 and called it in PowerShell like this:

.\test.ps1 “1″ “2″ “3″ 

Naturally I expected to see the string values “1″, “2″, and “3″ output to the console, each on their own line.

Instead, here is the output I received:

2
3

That’s odd.  Some arguments were output, but not all of them.  What happened to the first argument that I passed in?

In this example, when I execute the test.ps1 script, the PowerShell interpreter looks at the arguments I am passing in and attempts to assign them to the arguments that this script accepts as declared in the param statement.  I only have one argument in my param statement, so PowerShell takes the first argument that was passed in and converts it into the type of the argument declared in the param statement.  That means my “1″ string gets converted into an array of strings.  Then PowerShell looks at any remaining arguments and it stores them in an array in the $args variable.  This is a system variable that is used to store all remaining arguments that aren’t matched up to arguments declared in the param statement.

Looking at my example, you can see that I am using the $args variable in my param statement.  This means that when the script is run the first value is converted into an array of strings that is stored in the args variable.  Then the args variable is cleared and the remaining arguments are added to the $args variable array.  Once they are all added, the script is run and the last two arguments are output to the console.  As a result, the first argument gets discarded.

It is interesting to note that when you do this, the $args variable retains its array of strings type within the script.  By default, $args is an array of objects.  If you comment out the param statement in the script, the script will output all objects to the console, and the $args variable will store those objects in an array of type object as expected.

Lesson learned?  Don’t use the $args variable as an argument to a function or a script.

Kirk out.

Technorati Tags: , , ,


The PowerShell Matrix

October 5, 2007

You know that scene in “The Matrix” where they download programs into Neo’s mind so that he can learn more quickly?  I need to get myself one of those for PowerShell.

The more I learn about PowerShell the more I realize there is to learn and the more I want to know.  I am such a Poshoholic!

Kirk out.

Technorati Tags: , ,


Get-PSSnapin one-liners

October 1, 2007

With the number of PowerShell snap-ins that are available continuing to grow steadily, I’ve noticed more and more that people are looking for ways to determine if a snap-in was properly registered, whether snap-in changes are properly showing up in the PowerShell console, and answers to other general snap-in troubleshooting questions (note, a PowerShell snap-in is also referred to as a PSSnapin within the PowerShell console).  Fortunately, as is the nature with many things in PowerShell, there are a number of one-liners that we can use to gather information about snap-ins or to diagnose and troubleshoot snap-in problems.  I’ll list each one with the question that it was designed to answer below.

What snap-ins come with PowerShell?

Get-PSSnapin `
    | Where-Object { $_.IsDefault -eq $true }

What third-party snap-ins do I have on this system?

Get-PSSnapin -Registered

Which of the third-party snap-ins are already added to my current PowerShell session?

Get-PSSnapin `
    | Where-Object { $_.IsDefault -eq $false }

Which of the third-party snap-ins are not added to my current PowerShell session?

Get-PSSnapin -Registered `
    | Where-Object { (Get-PSSnapin $_.Name -ErrorAction SilentlyContinue) -eq $null }

How can I see all snap-ins and identify which ones come with PowerShell, which ones are third-party, which ones are added to my current PowerShell session and which ones are not (i.e. how do I put all of this information together)?

(Get-PSSnapin | Where-Object {$_.IsDefault -eq $true}) + (Get-PSSnapin -Registered) `
    | Add-Member -Force -Name IsAdded -MemberType ScriptProperty -Value {(Get-PSSnapin $this.Name -ErrorAction SilentlyContinue) -ne $null} {} -PassThru `
    | Format-List -Property Name,IsDefault,IsAdded,PSVersion,Description

I’m having general issues when using a third-party snap-in (cmdlets I’m trying to use aren’t found, changes made in a new version aren’t showing up in PowerShell, etc).  The snapin is called ProblematicSnapin.  How can I find out detailed information about the snap-in itself so that I can verify the information about the snap-in in PowerShell and troubleshoot the issues?  Note that for this one-liner you must replace “ProblematicSnapin” with the name of the third-party snap-in you are using.

Get-PSSnapin -Registered -Name ProblematicSnapin `
    | Add-Member -Force -Name IsAdded -MemberType ScriptProperty -Value {(Get-PSSnapin $this.Name -ErrorAction SilentlyContinue) -ne $null} {} -PassThru `
    | Format-List -Property *

What cmdlets are provided by a snap-in?  Note that for this one-liner you must replace “SnapinName” with the name of the snap-in you are inquiring about.  Also note that this only works for default snap-ins and snap-ins that have been added to the current PowerShell session.

Get-PSSnapin -Name SnapinName `
    | ForEach-Object { Get-Command -PSSnapin $_ } 

Hopefully these one-liners will help you get answers to your PSSnapin related questions.

Enjoy!

Kirk out.

Technorati Tags: , , , , ,