10 powershell mistakes, trips and traps
DESCRIPTION
Common mistakes and traps that catch many PowerShell beginners.TRANSCRIPT
![Page 1: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/1.jpg)
10 POWERSHELL MISTAKES, TRIP-UPS
AND TRAPSJEFFERY HICKS
PROF. POWERSHELL
![Page 2: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/2.jpg)
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
![Page 3: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/3.jpg)
#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
![Page 4: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/4.jpg)
#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
![Page 5: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/5.jpg)
#3 BOOLEANS ARE OBJECTS, TOO
• $True and $False are not strings• The cool PowerShell kids don’t compare
![Page 6: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/6.jpg)
#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
![Page 7: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/7.jpg)
#5 VARIABLE VULNERABILITY
• This isn’t VBScript• Variable names should be meaningful• Avoid re-using variables for different object types
![Page 8: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/8.jpg)
#6 POWERSHELL IS NOT .NET PROGRAMMING
• Use cmdlets wherever possible• .NET classes and methods are for exceptions• …or large scale performance benefits
![Page 9: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/9.jpg)
#7 POWERSHELL IS NOT VBSCRIPT
• PowerShell requires a new paradigm• Don’t turn VBScript into PowerShell• Objects in the pipeline
![Page 10: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/10.jpg)
#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
![Page 11: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/11.jpg)
#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
![Page 12: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/12.jpg)
#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
![Page 13: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/13.jpg)
QUESTIONS AND ANSWERS
![Page 14: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/14.jpg)
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
![Page 15: 10 PowerShell Mistakes, Trips and Traps](https://reader035.vdocuments.mx/reader035/viewer/2022062706/557d5fffd8b42ae1438b4f61/html5/thumbnails/15.jpg)
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