PowerShell Tips

Sometimes, you just need a quick pointer to a best practice or a quick solution, or a simple way to make your coding life better. Here are some of mine.

  • Passing code from one script to another or creating PowerShell with PowerShell? Don’t forget about ScriptBlocks. They’re like strings on steroids.
  • The PowerShell ISE is your friend. Visual Studio Code is also your friend.
  • Whitespace is valuable
  • JSON is not a configuration language. It is a serialization format.
  • Write-Verbose is worth its weight in gold. So is Write-Warning and Write-Debug
  • Get-Help is the most important Cmdlet in PowerShell
  • Minimise inputs – include sensible defaults to allow power users maximum control with a minimal version
  • Make your output machine-consumable where you can (subject to the next tip)
  • Make your code idempotent and quick-to-return where you can (subject to the previous tip)
  • Pester is mandatory. If you’re not developing with tests, you’re developing with Hitler.
  • Think with scale. How will your code work if plugged into an execution engine and run over and over by a robot? Sure, it’s not happening now, but it might
  • Remember Open Source is a thing. Feel free to re-use other people’s code – while following the licence – and in turn publish your own code. Working in isolation is all very well but building on the community is better
  • If your code requires a human to type complicated strings into a command line, or to build a complicated standalone.xml or other config file, then your code is not ready for Modern Windows Ops. Minimise repeated human input.

Leave a Reply

Your email address will not be published. Required fields are marked *