10 POWERSHELL MISTAKES, 
TRIP-UPS AND TRAPS 
JEFFERY HICKS 
PROF. POWERSHELL
WHO AM I? 
• Windows PowerShell MVP 
• PowerShell Author 
• Learn PowerShell 3 in a Month of Lunches (with Don Jones) 
• Learn PowerShell Toolmaking in a Month of Lunches (with Don Jones) 
• PowerShell in Depth (with Don Jones & Richard Siddaway) 
• Managing Active Directory with Windows PowerShell: TFM 2nd Ed. 
• IT trainer and consultant 
• http://jdhitsolutions.com/blog 
• http://twitter.com/jeffhicks
#1 DON’T USE WRITE-HOST 
• Write-Host writes to the console, not the pipeline 
• You can only work with objects written to the pipeline 
• Puppies are hurt when you use Write-Host 
• Unless you use as intended
#2 IT’S ALL ABOUT THE OBJECTS 
• Don’t try to parse text 
• Don’t try to concatenate 
• Everything in PowerShell is an object 
• Learn how to use Get-Member
#3 BOOLEANS ARE OBJECTS, TOO 
• $True and $False are not strings 
• The cool PowerShell kids don’t compare
#4 DON’T FALL INTO THE ALIAS TRAP 
• Aliases are fine for interactive sessions 
• You know what you’re typing 
• Aliases disguise the real command
#5 VARIABLE VULNERABILITY 
• This isn’t VBScript 
• Variable names should be meaningful 
• Avoid re-using variables for different object types
#6 POWERSHELL IS NOT .NET PROGRAMMING 
• Use cmdlets wherever possible 
• .NET classes and methods are for exceptions 
• …or large scale performance benefits
#7 POWERSHELL IS NOT VBSCRIPT 
• PowerShell requires a new paradigm 
• Don’t turn VBScript into PowerShell 
• Objects in the pipeline
#8 DON’T MAKE ERRORS GO AWAY 
• Don’t turn off error pipeline 
• $ErrorActionPreference = "SilentlyContinue" 
• Makes troubleshooting more difficult 
• Use Try/Catch and learn how to handle errors
#9 DON’T RE-INVENT THE WHEEL 
• Don’t try to duplicate PowerShell functionality 
• No need to write your own help routine 
• Don’t try to re-invent cmdlets using .NET
#10 NOT EVERYTHING IS A ONE-LINER 
• One-line commands can be efficient 
• Great for interactive console 
• Not necessarily a benefit in scripting 
• Don’t code yourself into a corner
QUESTIONS AND ANSWERS
RECOMMENDATIONS 
• Write objects to the pipeline 
• Use cmdlets where possible 
• No aliases or shortcuts in scripts 
• Learn how to handle errors 
• Read the help and examples
RESOURCES 
• Learn PowerShell 3 in a Month of Lunches by Don Jones and Jeffery 
Hicks 
• Learn PowerShell Toolmaking in a Month of Lunches by Don Jones 
and Jeffery Hicks 
• The Lonely Administrator (http://jdhitsolutions.com/blog) 
• Prof. PowerShell (http://mcpmag.com/articles/list/prof-powershell. 
aspx 
• PowerShell.org
THANK YOU 
http://jdhitsolutions.com/blog 
jhicks@jdhitsolutions.com 
@JeffHicks 
http://gplus.to/jeffhicks

10 PowerShell Mistakes, Trips and Traps

  • 1.
    10 POWERSHELL MISTAKES, TRIP-UPS AND TRAPS JEFFERY HICKS PROF. POWERSHELL
  • 2.
    WHO AM I? • Windows PowerShell MVP • PowerShell Author • Learn PowerShell 3 in a Month of Lunches (with Don Jones) • Learn PowerShell Toolmaking in a Month of Lunches (with Don Jones) • PowerShell in Depth (with Don Jones & Richard Siddaway) • Managing Active Directory with Windows PowerShell: TFM 2nd Ed. • IT trainer and consultant • http://jdhitsolutions.com/blog • http://twitter.com/jeffhicks
  • 3.
    #1 DON’T USEWRITE-HOST • Write-Host writes to the console, not the pipeline • You can only work with objects written to the pipeline • Puppies are hurt when you use Write-Host • Unless you use as intended
  • 4.
    #2 IT’S ALLABOUT THE OBJECTS • Don’t try to parse text • Don’t try to concatenate • Everything in PowerShell is an object • Learn how to use Get-Member
  • 5.
    #3 BOOLEANS AREOBJECTS, TOO • $True and $False are not strings • The cool PowerShell kids don’t compare
  • 6.
    #4 DON’T FALLINTO THE ALIAS TRAP • Aliases are fine for interactive sessions • You know what you’re typing • Aliases disguise the real command
  • 7.
    #5 VARIABLE VULNERABILITY • This isn’t VBScript • Variable names should be meaningful • Avoid re-using variables for different object types
  • 8.
    #6 POWERSHELL ISNOT .NET PROGRAMMING • Use cmdlets wherever possible • .NET classes and methods are for exceptions • …or large scale performance benefits
  • 9.
    #7 POWERSHELL ISNOT VBSCRIPT • PowerShell requires a new paradigm • Don’t turn VBScript into PowerShell • Objects in the pipeline
  • 10.
    #8 DON’T MAKEERRORS GO AWAY • Don’t turn off error pipeline • $ErrorActionPreference = "SilentlyContinue" • Makes troubleshooting more difficult • Use Try/Catch and learn how to handle errors
  • 11.
    #9 DON’T RE-INVENTTHE WHEEL • Don’t try to duplicate PowerShell functionality • No need to write your own help routine • Don’t try to re-invent cmdlets using .NET
  • 12.
    #10 NOT EVERYTHINGIS A ONE-LINER • One-line commands can be efficient • Great for interactive console • Not necessarily a benefit in scripting • Don’t code yourself into a corner
  • 13.
  • 14.
    RECOMMENDATIONS • Writeobjects to the pipeline • Use cmdlets where possible • No aliases or shortcuts in scripts • Learn how to handle errors • Read the help and examples
  • 15.
    RESOURCES • LearnPowerShell 3 in a Month of Lunches by Don Jones and Jeffery Hicks • Learn PowerShell Toolmaking in a Month of Lunches by Don Jones and Jeffery Hicks • The Lonely Administrator (http://jdhitsolutions.com/blog) • Prof. PowerShell (http://mcpmag.com/articles/list/prof-powershell. aspx • PowerShell.org
  • 16.
    THANK YOU http://jdhitsolutions.com/blog [email protected] @JeffHicks http://gplus.to/jeffhicks

Editor's Notes

  • #2 Windows PowerShell is a language and management technology that many IT professionals, including developers, think they understand. Yet very often they get caught up in pre-conceptions and misinterpretations, usually based on prior experience with scripting or development. This session will explore the 10 most common mistakes and traps that people fall into with PowerShell and how to avoid them.