How do I pass an array parameter to powershell -file?

Question:

I have the following powershell script:

If I execute .\ParameterTest.ps1 (1..10) I get the following:

However, I don’t get the expected results if I run it in a separate powershell instance like this powershell -file .\ParameterTest.ps1 (1..10). In that case I get the following:

My question is how can I pass the array, or other complex data type from a command line?

Answer:

The answer is to use powershell.exe -EncodedCommand and to base64 encode the parameters. The description for this is on the PowerShell.exe Console Help technet page. I have compressed their version of the ceremony to do this into a one-liner:

Source:

How do I pass an array parameter to powershell -file? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply