Tuesday, March 12, 2013

Little Known PowerShell Tips & Tricks


  1. PowerShell script blocks can contain a begin, process and end script block as described here: http://technet.microsoft.com/en-us/magazine/hh413265.aspx or here: http://mjolinor.wordpress.com/2012/03/11/begin-process-end-not-just-for-functions/
  2. In order to access Microsoft.NET Static methods, you can use the double colon notation in front of the respective Microsoft.NET Class such as described here: http://technet.microsoft.com/en-us/library/dd347632.aspx
  3. You can embed C# class within PowerShell scripts as described here: http://blogs.technet.com/b/stefan_gossner/archive/2010/05/07/using-csharp-c-code-in-powershell-scripts.aspx
  4. You can even assign script blocks to variables as described here: http://ss64.com/ps/syntax-variables.html
  5. You can write out long strings using Here-Strings as described here: http://technet.microsoft.com/en-us/library/ee692792.aspx and here: http://blogs.msdn.com/b/powershell/archive/2006/07/15/variable-expansion-in-strings-and-herestrings.aspx
  6. You can send output to the console as well as a variable as described here: http://blogs.technet.com/b/heyscriptingguy/archive/2013/02/23/powertip-easily-send-output-to-both-the-powershell-console-and-a-variable.aspx
  7. You can get a listing of PowerShell command aliases as described here: http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/15/weekend-scripter-discovering-powershell-cmdlet-parameter-aliases.aspx
  8. You can define parameters explicitly (instead of using $args variable) as described here: http://technet.microsoft.com/en-us/magazine/jj554301.aspx
  9. You can validate your script parameters as described here: http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/15/simplify-your-powershell-script-with-parameter-validation.aspx
  10. You can use Invoke-Expression or Invoke-Command to command presented as strings as described here: http://richardspowershellblog.wordpress.com/2011/06/14/powershell-basics-invoking-commands/
  11. You can test out your commands before actually executing them by using the -whatif parameter as described here: http://www.computerperformance.co.uk/powershell/powershell_whatif_confirm.htm.  You can also easily add -whatif support to your own PowerShell functions as outlined here: http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/08/weekend-scripter-easily-add-whatif-support-to-your-powershell-functions.aspx
  12. To work with Key/Value Pair Collections in PowerShell, you can use Hash Tables: http://technet.microsoft.com/en-us/library/ee692803.aspx

No comments:

Post a Comment