Storing multiple commands in an array and running them

Hey, I am trying to write a script to store multiple commands in an array and run them. I can get the command to work but whenever I try to reference the elements I get an error that says “cannot index into a null array”. I’m wondering what might cause this and if anyone can take a look at my script.

Please do not post images of code as this is not helpful at all. Instead post the plain text and format it as code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:

You can use a script block to save multiple commands in one variable.

A few points:

  1. Your commands need to be enclosed in quotes
  2. When running the first command you would need to run Invoke-Expression $Commands[0] - Typically Invoke-Expression is frowned on from a security perspective just be aware of the risk
  3. Why not just run the commands directly and add things like error checking, etc