Deciding the best container for your scripts

When you are scripting, it is important to put consideration into what kind of container you will be using. Sometimes the simplicity of creating a singular variable and updating that variable is less complex than creating an array or hash table to search through. At other times, it may be more efficient to pull the whole dataset and use individual pieces of that data within your script.

Single-line variables can be used for:

  • Math operations that require calculations of single or multiple values
  • Catching single-line output from executing a non-PowerShell command
  • Tracking current position in a loop like "percent complete"

Arrays are best used for:

  • Storing a list of items for individual processing within a script ...

Get Mastering Windows PowerShell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.