[NOTES]: Learn Windows PowerShell 3 in a Month of Lunches – Chapter 4 Notes.

Anatomy of a PowerShell command.

Cmdlet Name Parameter 1 Parameter2 Parameter3
Get-EventLog -LogName Security -ComputerName Win8, server1 -Verbose
Parameter Name Parameter Value Parameter Name Parameter Value Switch Parameter

 

  • Parameter values that do not contain punctuation or spaces do not need to be inside quotation marks.
  • Parameter values may be in comma-separated lists, such as the param value for Parameter 2 in the above example.
  • Parameter names always start with a dash. (-)
  • Mandatory space between cmdlet name and first parameter.
  • Case sensitivity not required.
  • Cmdlet = native PowerShell CLI
  • Term “cmdlet” is unique to PowerShell
  • Function != cmdlet, but are similar.
    • They are not written in .NET, but rather, PowerShell’s own scripting language.
  • A “workflow” is a special type of function. They tie into PowerShell’s workflow execution process.
  • An “application” is an external executable – i.e. ping, ipconfig, etc.
  • When developing functions and workflows, it is generally good practice to apply Microsoft’s established naming convention in the same way that MS applies them to cmdlets.
  • Anatomy of naming convention: Verb-SingularNoun (i.e Get-EventLog).
  • To get a list of acceptable verbs, run: Get-Verb.
  • Like many other shells, and scripting languages, you can alias commands.
  • There are some pre-defined aliases. Try running Get-Alias -Definition Get-TimeZone.
    • Output for above will be: gtz.
  • You can get help for aliases as well, try: help gtz.
  • You can also create local aliases.
  • Local aliases are only good for the single shell session they’re created in. When you exit PowerShell, they’re gone.
  • To create a local alias, use: New-Alias.
    • Example: New-Alias -Name “time” Get-TimeZone.
  • Parameters can be aliased, too. Parameter aliases are not as convenient to find.
    • (get-command [Command Name] | select ExpandProperty parameters).[Parameter Name].aliases
  • Positional parameters are wrapped in square brackets – but the positional value can also be easily found with the -full switch on help.
  • The Show-Command cmdlet provides the administrator with a GUI to simplify building complex commands.
    • Rather than hitting RUN in the Show-Command GUI, hit COPY instead, to paste it manually into the shell. This allows you to see the command syntax in its proper form, to learn from.
  • PowerShell supports many traditional external commands that most system administrators are already used to. No need to drop the shell and go over to CMD to run something like “ping” or “ipconfig” for instance.
    • With that said, Microsoft has developed many commands that can be used to replace older commands – often with greater output or better features. One instance of this is test-connection to replace ping.
    • Still, there are some external commands that may not parse correctly. One way to work around this is to pass all of the elements that make up the external command into PowerShell variables (for instance $varName), and then run the external command using the variables you defined.
  • A parameter that takes no value – for instance: -recurse – gets a dash before its name.
Tyler
Written by Tyler