Powershell: The Not So Obvious

These are a list of things that, to me, seem different than C# or not obvious. Otherwise you should be able to follow very similar to C#.

  1. Cmdlets follow a standard “Verb-Noun” naming convention.
  2. Get help for a particular command:
    • get-help [cmdlet]
  3. To filter most any results you can:
    • use where-object and the property of the object needs to be referenced as $_.[propertyname]
  4. Get all PowerShell commands:
    • get-command | where-object { $_.commandtype -eq “cmdlet”}
  5. F7 shows the history buffer
  6. Useful parameters most all cmdlets have:
    • -whatif – Cmdlet is not actually executed, provides information about “what would happen” if executed.
    • -confirm – Prompt user before executing cmdlet.
    • -Verbose – Provides more detail.
    • -debug – Provides debugging information.
    • -ErrorAction – Instructs cmdlet to perform an action when errors occur. Such as: continue, stop, silently continue, and inquire.
    • -ErrorVariable – Use a specific variable to hold error information. This is in addition to the standard $error variable.
    • -OutVariable – Variable used to hold output information.
    • -OutBuffer – Hold a certain number of objects before calling the next cmdlet in the pipeline.
  7. To make a comment in PowerShell use #.
  8. PowerShell allows you to work with more than just the file system.
  9. To get a list of providers:
    • Get-PSProvider
  10. To get a list of drives to connect to:
    • Get-PSDrive
  11. To connect to that drive:
    • Set-Location D:
  12. Common Special Variable Examples:
    • $_ – Contains the current pipeline object, used in script blocks, filters, and the where statement.
    • $Args – Contains an array of the parameters passed to a function.
    • $Error – Contains objects for which an error occurred while being processed in a cmdlet.
    • $Home – Specifies the user’s home directory.
    • $PsHome – The directory where the Windows PowerShell is installed.
    • Get-Help about_automatic_variables – To see them all
  13. How to create an array (hash value work the same with an equal sign):
    • $arrayVariable = @(List of comma separated values)
  14. Conditional Logic:
    • -eq – Equal to
    • -lt  – Less than
    • -gt  – Greater than
    • -ge  – Greater than or Eqaul to
    • -le  – Less than or equal to
    • -ne  – Not equal to
  15. The switch statement doesn’t use the word case.
  16. To denote the end of a line or command use a semicolon, however this is only required if you’re trying to do multiple commands on one line (to my knowledge).
  17. You can create your own PowerShell functions.
    • Use the keyword “Function”.
  18. Final thing is building your code library. Since you can create functions for things you commonly do you might want to just create some files to manage these functions and then reference them from the current script you are creating. Here’s a good explanation of how to use and reference functions.
Most of this information was compiled from the PowerShell Pro website. However, I took things that I found important, added my own things that I already knew to create this post.
Tagged with: