Windows PowerShell Core Skills Tutorial Don Jones ConcentratedTech.com Learn Windows PowerShell in 2 Days / Part 1
Welcome! This is one of four sessions designed to teach specific Windows PowerShell skills Don Jones Windows PowerShell MVP Award Recipient PowerShell Columnist for  Microsoft TechNet Magazine “ Decision Maker” Columnist for  Redmond Magazine Author,  Learn Windows PowerShell in a Month of Lunches Co-Author,  Windows PowerShell v2.0: TFM Creator of numerous self-paced PowerShell training videos for CBTNuggets.com.
Caution: Demos Ahead! Please note:  This session is built primarily around demonstrations, and answering your questions. You won’t see many slides. If you’re looking for additional written resources, there are lots to choose from (including many free ones) – the closing slide will list some key URLs. We’ll be using slides mainly to form the agenda and for some conceptual stuff.
CAUTION: DEPLOY AIRBAGS! This is very much a “crash course.” We’re going to cover a lot, and we’re gonna do it quickly. Feel free to ask questions… but understand the value in “just being exposed to it.” You’ll really get the best comprehension when you start  using  these techniques in your job. So plan to do that when you go home!
Bonus I’ll post any scripts, as well as these slides. Download location will be listed at the end of this session. Don’t bother copying down commands – you’ll be able to download the whole session!
A Plug Much of what we’re covering is based on  Learn Windows PowerShell in a Month of Lunches There’s also a companion DVD that provides 99 video demos that correspond to the book I have copies with me if you’d like to take a look at them… or maybe acquire one…   Each physical copy includes a free ebook edition!
What is PowerShell? It isn’t a scripting language. It’s a command-line shell… … and like many shells, it  contains  a scripting language.  For right now, we’re going to focus on using it as a  shell.  It works a lot like the shell (Cmd.exe) you’re hopefully used to. Let’s see.
Things We Need to Cover System requirements (and the future) Cmdlets (and their names) Parameters Aliases PSProviders and PSDrives The Help System (and why not to use Get-Help) Get-Command
Parameter Confusion Parameter names can be  truncated , so long as you type enough for the shell to uniquely identify the parameter. Some parameters are  positional,  which means you can omit the parameter name if the value goes into the right spot. Get in the habit of using tab completion in the shell. I’ll show you. Also stick with always typing parameter names for now – it’s less confusing. When you use the parameter names, order doesn’t matter.
Connecting Commands Ever run  Dir | More ? If so, you know what the pipe does: It takes the output of one command and sends it to the input of the next command PowerShell can do the same thing, but it’s much more powerful (and a bit more complicated) Let’s see a couple of brief examples
Things to Cover -WhatIf and –Confirm Piping  and  Parameters (Out-File) The other Out- cmdlets Export cmdlets ConvertTo cmdlets
But Wait, There Must be More Run Get-Service… that can’t possibly be all PowerShell knows about services Let’s talk about “tables of data.” And also the Get-Member cmdlet. And also some proper terminology: Table =  Collection of objects Row =  Object Column =  Property Properties + Methods =  Members of an Object
Some Core Commands Sorting (Sort or Sort-Object) Filtering (Where or Where-Object) Picking specific properties (Select or Select-Object) This is a good time for some audience participation.
Let’s Play with Formatting PowerShell has pre-defined “views” for many types of objects – it will use those if you don’t specify something different The Format- cmdlets produce a special kind of object designed for output formatting Only Out-Host, Out-Printer, and Out-File can really make sense of those special objects Moral: If you use a Format- cmdlet, it  needs to be the last thing on the command-line .  Let’s see that, and a common “gotcha.”
Adding Commands Like the MMC, PowerShell’s main use comes when you add stuff to it. There’s a 3-step process for adding commands, finding what you added, and learning how to use them. Those 3 steps apply to the two mechanisms used to extend the shell.
Extending PowerShell PSSnapins (v1+) Get-PSSnapin –registered Add-PSSnapin  name Get-Command  –pssnapin  name Modules (v2+) Get-Module -ListAvailable Import-Module  name Get-Command -module  name Note:  It won’t list modules not installed in the correct path, but you can import using a path instead of just a name
Let’s Dig into the Pipeline More When you pipe something to a command, there’s no magical way for that command to accept that piped-in input All command input must be through a parameter So when you pipe something, PowerShell has to figure out what parameter to attach it to
Pipeline Input Plan A: ByValue Look at the TypeName of the object in the pipeline Does the receiving command have a parameter that accepts  that type  of data  from the pipeline  using the  ByValue  technique? If so, then that parameter gets the piped-in input
Plan B: ByPropertyName Only used when Plan A (ByValue) doesn’t work out Look at each property of the piped-in object(s) See if the receiving command has parameters that accept pipeline input  ByPropertyName For each receiving parameter whose name matches a property name of the input object, connect the matches
ByPropertyName Get-Service Name Status DisplayName (etc) Stop-Process Name* ID* InputObject (etc) *Accepts ByPropertyName MATCH!
Remember… The help files can  help  you to figure out what’s possible Generally, commands with the same noun will connect “properly” But you can be creative… let’s see an example using AD
Real World? Where is that CSV file likely to come from?
Fix it in the Pipeline Get-Whatever | Select-Object @{   name='wanted-property-name';   expression={$_.original-property-name} } | Do-Whatever
Or… Get-Whatever | Select-Object @{   n ='wanted-property-name';   e ={$_.original-property-name} } | Do-Whatever
Or… Get-Whatever | Select-Object @{   label ='wanted-property-name';   expression ={$_.original-property-name} } | Do-Whatever
Or… Get-Whatever | Select-Object @{   l ='wanted-property-name';   e ={$_.original-property-name} } | Do-Whatever
And Hey! You can use that same structure with Format-List and Format-Table With Format-Table you can use these keys… Expression (e) Label or Name (l or n) FormatString (N0, etc) Align (left/right) But remember: Format- goes last. So if you need to modify  and then use  an object, do it with Select-Object not Format-.
Comparisons and Filtering -eq -ne  -lt -le -gt -ge -not -and -or (parentheses) group expressions Within the –FilterScript of Where-Object, use $_ to represent “whatever was piped in”
WMI Egh. Get-WmiObject if your friend. A decent “WMI Explorer” tool is even more your friend. But let’s play with Get-WmiObject
Things to Cover… Listing classes Finding namespaces Examining classes with Get-Member Remote computers (and the protocol) Alternate credentials and authentication and a trick for credential management
Working with Bunches A “Batch” cmdlet (like Stop-Service or Restart-Computer) A WMI method that works with Invoke-WmiMethod (like Win32Shutdown() of Win32_OperatingSystem) ForEach-Object (a bit yucky – Change() of Win32_Service)
PowerShell Security Must type a path .PS1 filename association Execution Policy Unrestricted (dumb) RemoteSigned (less dumb) AllSigned (smarter) Restricted (default) Bypass (specialized) Also: PowerShell.exe switches, GPOs, etc.
Input and Output Write-Verbose Write-Debug Write-Output Read-Host Write-Host
An Intro to Variables $ is a shell cue to access the contents of a variable $var  accesses the contents of var – but the variable  name  is var, not $var ${this is a legal variable name, sadly} The VARIABLE: drive and the –Variable cmdlets [typing]$variables 'Tricks' with "Quotes" and `Escapes
A Simple, Parameterized Script Reference: [CmdletBinding()] param( [string]$whatever = 'default' )
Well, That Was Just a Lot. The story continues in the other three sessions in this series… won’t you join me tomorrow?
Thank You! Please feel free to hit me up with any remaining questions between sessions Please submit a session evaluation!  These are an extremely important part of ensuring that the conference continues to provide you with the education you need! Resources and URLs on the next slide…
Remember! Slides, and scripts will be posted within a few days. You’re welcome to download whatever you like; please don’t re-post anything elsewhere on the Internet. For the download URL, see my Twitter feed: @concentrateddon Twitter.com/concentrateddon
More PowerShell Resources Web Sites ShellHub.com Bit.ly/DonJones YouTube.com/ ConcentratedDon ITPro. ConcentratedTech.com Thank you again for attending! Available Here!

PowerShell Core Skills (TechMentor Fall 2011)

  • 1.
    Windows PowerShell CoreSkills Tutorial Don Jones ConcentratedTech.com Learn Windows PowerShell in 2 Days / Part 1
  • 2.
    Welcome! This isone of four sessions designed to teach specific Windows PowerShell skills Don Jones Windows PowerShell MVP Award Recipient PowerShell Columnist for Microsoft TechNet Magazine “ Decision Maker” Columnist for Redmond Magazine Author, Learn Windows PowerShell in a Month of Lunches Co-Author, Windows PowerShell v2.0: TFM Creator of numerous self-paced PowerShell training videos for CBTNuggets.com.
  • 3.
    Caution: Demos Ahead!Please note: This session is built primarily around demonstrations, and answering your questions. You won’t see many slides. If you’re looking for additional written resources, there are lots to choose from (including many free ones) – the closing slide will list some key URLs. We’ll be using slides mainly to form the agenda and for some conceptual stuff.
  • 4.
    CAUTION: DEPLOY AIRBAGS!This is very much a “crash course.” We’re going to cover a lot, and we’re gonna do it quickly. Feel free to ask questions… but understand the value in “just being exposed to it.” You’ll really get the best comprehension when you start using these techniques in your job. So plan to do that when you go home!
  • 5.
    Bonus I’ll postany scripts, as well as these slides. Download location will be listed at the end of this session. Don’t bother copying down commands – you’ll be able to download the whole session!
  • 6.
    A Plug Muchof what we’re covering is based on Learn Windows PowerShell in a Month of Lunches There’s also a companion DVD that provides 99 video demos that correspond to the book I have copies with me if you’d like to take a look at them… or maybe acquire one…  Each physical copy includes a free ebook edition!
  • 7.
    What is PowerShell?It isn’t a scripting language. It’s a command-line shell… … and like many shells, it contains a scripting language. For right now, we’re going to focus on using it as a shell. It works a lot like the shell (Cmd.exe) you’re hopefully used to. Let’s see.
  • 8.
    Things We Needto Cover System requirements (and the future) Cmdlets (and their names) Parameters Aliases PSProviders and PSDrives The Help System (and why not to use Get-Help) Get-Command
  • 9.
    Parameter Confusion Parameternames can be truncated , so long as you type enough for the shell to uniquely identify the parameter. Some parameters are positional, which means you can omit the parameter name if the value goes into the right spot. Get in the habit of using tab completion in the shell. I’ll show you. Also stick with always typing parameter names for now – it’s less confusing. When you use the parameter names, order doesn’t matter.
  • 10.
    Connecting Commands Everrun Dir | More ? If so, you know what the pipe does: It takes the output of one command and sends it to the input of the next command PowerShell can do the same thing, but it’s much more powerful (and a bit more complicated) Let’s see a couple of brief examples
  • 11.
    Things to Cover-WhatIf and –Confirm Piping and Parameters (Out-File) The other Out- cmdlets Export cmdlets ConvertTo cmdlets
  • 12.
    But Wait, ThereMust be More Run Get-Service… that can’t possibly be all PowerShell knows about services Let’s talk about “tables of data.” And also the Get-Member cmdlet. And also some proper terminology: Table = Collection of objects Row = Object Column = Property Properties + Methods = Members of an Object
  • 13.
    Some Core CommandsSorting (Sort or Sort-Object) Filtering (Where or Where-Object) Picking specific properties (Select or Select-Object) This is a good time for some audience participation.
  • 14.
    Let’s Play withFormatting PowerShell has pre-defined “views” for many types of objects – it will use those if you don’t specify something different The Format- cmdlets produce a special kind of object designed for output formatting Only Out-Host, Out-Printer, and Out-File can really make sense of those special objects Moral: If you use a Format- cmdlet, it needs to be the last thing on the command-line . Let’s see that, and a common “gotcha.”
  • 15.
    Adding Commands Likethe MMC, PowerShell’s main use comes when you add stuff to it. There’s a 3-step process for adding commands, finding what you added, and learning how to use them. Those 3 steps apply to the two mechanisms used to extend the shell.
  • 16.
    Extending PowerShell PSSnapins(v1+) Get-PSSnapin –registered Add-PSSnapin name Get-Command –pssnapin name Modules (v2+) Get-Module -ListAvailable Import-Module name Get-Command -module name Note: It won’t list modules not installed in the correct path, but you can import using a path instead of just a name
  • 17.
    Let’s Dig intothe Pipeline More When you pipe something to a command, there’s no magical way for that command to accept that piped-in input All command input must be through a parameter So when you pipe something, PowerShell has to figure out what parameter to attach it to
  • 18.
    Pipeline Input PlanA: ByValue Look at the TypeName of the object in the pipeline Does the receiving command have a parameter that accepts that type of data from the pipeline using the ByValue technique? If so, then that parameter gets the piped-in input
  • 19.
    Plan B: ByPropertyNameOnly used when Plan A (ByValue) doesn’t work out Look at each property of the piped-in object(s) See if the receiving command has parameters that accept pipeline input ByPropertyName For each receiving parameter whose name matches a property name of the input object, connect the matches
  • 20.
    ByPropertyName Get-Service NameStatus DisplayName (etc) Stop-Process Name* ID* InputObject (etc) *Accepts ByPropertyName MATCH!
  • 21.
    Remember… The helpfiles can help you to figure out what’s possible Generally, commands with the same noun will connect “properly” But you can be creative… let’s see an example using AD
  • 22.
    Real World? Whereis that CSV file likely to come from?
  • 23.
    Fix it inthe Pipeline Get-Whatever | Select-Object @{ name='wanted-property-name'; expression={$_.original-property-name} } | Do-Whatever
  • 24.
    Or… Get-Whatever |Select-Object @{ n ='wanted-property-name'; e ={$_.original-property-name} } | Do-Whatever
  • 25.
    Or… Get-Whatever |Select-Object @{ label ='wanted-property-name'; expression ={$_.original-property-name} } | Do-Whatever
  • 26.
    Or… Get-Whatever |Select-Object @{ l ='wanted-property-name'; e ={$_.original-property-name} } | Do-Whatever
  • 27.
    And Hey! Youcan use that same structure with Format-List and Format-Table With Format-Table you can use these keys… Expression (e) Label or Name (l or n) FormatString (N0, etc) Align (left/right) But remember: Format- goes last. So if you need to modify and then use an object, do it with Select-Object not Format-.
  • 28.
    Comparisons and Filtering-eq -ne -lt -le -gt -ge -not -and -or (parentheses) group expressions Within the –FilterScript of Where-Object, use $_ to represent “whatever was piped in”
  • 29.
    WMI Egh. Get-WmiObjectif your friend. A decent “WMI Explorer” tool is even more your friend. But let’s play with Get-WmiObject
  • 30.
    Things to Cover…Listing classes Finding namespaces Examining classes with Get-Member Remote computers (and the protocol) Alternate credentials and authentication and a trick for credential management
  • 31.
    Working with BunchesA “Batch” cmdlet (like Stop-Service or Restart-Computer) A WMI method that works with Invoke-WmiMethod (like Win32Shutdown() of Win32_OperatingSystem) ForEach-Object (a bit yucky – Change() of Win32_Service)
  • 32.
    PowerShell Security Musttype a path .PS1 filename association Execution Policy Unrestricted (dumb) RemoteSigned (less dumb) AllSigned (smarter) Restricted (default) Bypass (specialized) Also: PowerShell.exe switches, GPOs, etc.
  • 33.
    Input and OutputWrite-Verbose Write-Debug Write-Output Read-Host Write-Host
  • 34.
    An Intro toVariables $ is a shell cue to access the contents of a variable $var accesses the contents of var – but the variable name is var, not $var ${this is a legal variable name, sadly} The VARIABLE: drive and the –Variable cmdlets [typing]$variables 'Tricks' with "Quotes" and `Escapes
  • 35.
    A Simple, ParameterizedScript Reference: [CmdletBinding()] param( [string]$whatever = 'default' )
  • 36.
    Well, That WasJust a Lot. The story continues in the other three sessions in this series… won’t you join me tomorrow?
  • 37.
    Thank You! Pleasefeel free to hit me up with any remaining questions between sessions Please submit a session evaluation! These are an extremely important part of ensuring that the conference continues to provide you with the education you need! Resources and URLs on the next slide…
  • 38.
    Remember! Slides, andscripts will be posted within a few days. You’re welcome to download whatever you like; please don’t re-post anything elsewhere on the Internet. For the download URL, see my Twitter feed: @concentrateddon Twitter.com/concentrateddon
  • 39.
    More PowerShell ResourcesWeb Sites ShellHub.com Bit.ly/DonJones YouTube.com/ ConcentratedDon ITPro. ConcentratedTech.com Thank you again for attending! Available Here!

Editor's Notes

  • #2 TechMentor Las Vegas 2011
  • #3 TechMentor Las Vegas 2011