Sunday, May 30, 2010

Windows PowerShell: What you absolutely need to know

http://searchwindowsserver.techtarget.com/tip/0,289483,sid68_gci1513245,00.html?ShortReg=1&mboxConv=searchWindowsServer_RegActivate_Submit&


Now that you are convinced that Windows PowerShell is the best thing since slice bread -- you are convinced, right? -- let's take some time to tackle the hurdle that slows most people down: discovery. In other words, how do you find what you need without lengthy Google/Bing searches?

The challenge with any new technology involves learning how to learn it. Every technology has its own concepts and terminology that make it unique, and understanding these concepts is generally the biggest obstacle you'll face.

The creators of Windows PowerShell truly understood this and added some built-in cmdlets to help with learning PowerShell. I will go over the top five such cmdlets in this article.

Before we get to that though, let's look at some of the basic PowerShell terminology for those who are really new to it.

  • Cmdlets -- These are the foundation of Windows PowerShell as well as the source of most confusion. They are like both intrinsic and native commands, but they are also neither. I find it easiest to just think of them as small little snack packs of code that are available regardless of location.
  • Functions -- These are very similar to cmdlets except they are generated on the fly using the key work "function".

    Example: function foo {"I'm Foo"}

  • Parameters -- Named value passed to a cmdlet or function.
  • Objects -- Item(s) returned from cmdlets/functions. Objects have both properties and methods. Speaking of which…
  • Properties -- Attributes that are used to describe an object.
  • Methods -- Actions performed by an object.
  • Variables -- These are objects used to store data.
  • Pipelines -- This refers to the concept of passing object(s) from one command to another. This is done by using the pipeline operator "|" .
  • Aliases -- Gives users the ability to provide abbreviations for cmdlets.
  • ScriptBlock -- A block of code wrapped in "{" and "}" .

    Example: {Write-Host "This is a ScriptBlock"}

Now let's start the breakdown of important cmdlets. The characters in parenthesis are the aliases for each cmdlet:

  • Get-Help (help:) -- As you learn Windows PowerShell, this cmdlet will be your bread and butter. As you can guess, it provides help for cmdlets or PowerShell concepts.

    If you want to know more about something, you can start by simply passing it to Get-Help. This will provide you with a direct response or give you a list of help files that apply (PowerShell 2.0-only.) Get-Help has three parameters that control the amount of data returned: Detail, Full, and Example.

    Syntax: Get-help
    Example: Get-Help Get-Member

  • Get-Member (gm:) -- This is arguably PowerShell's most important cmdlet. It allows you to "discover" what properties and methods a given object has. After all, you can't do very much with properties if you don't know they are there.

    Syntax: | get-member
    Example: Get-ChildItem | Get-Member

  • Get-Command (gcm:) -- By now I am sure you are seeing the pattern. Get-Command simply gets you commands. These commands can be cmdlets, functions, aliases, and applications.

    Syntax: Get-Command –commandtype
    Example: Get-Command –commandtype cmdlet

  • Foreach-Object (%:) -- This cmdlet is used to process items in a pipeline. For each object in the pipeline it will process script, block inserting the current object in place of "$_".

    Syntax: | foreach-object
    Example: Get-Childitem | foreach-object {Write-Host "Found: " $_.fullname}

  • Where-Object (?:) -- This is similar to Foreach-Object, but instead of simply processing the script block it uses the script block as a sort of filter. If the result of the script block is $true, the object is passed on. If the result is $false, the current object is dropped.

    Syntax: | where-object
    Example: Get-ChildItem | where-object {$_.Length –gt 10mb}

Again, these are only a few cmdlets, but I think they do a good job of getting one started learning Windows PowerShell. Now you can go out there and conquer the world given your new PowerShell skills.


No comments:

Post a Comment