Need Help Structuring Parameters in a PowerShell Script

October 21, 2025 5 answers
Asked by cinnamon+305
Hey everyone! I'm working on a utility script that needs to handle different types of parameters in a flexible way. The script requires one mandatory positional parameter for the environment and has an optional positional parameter for color, plus a non-positional parameter that should accept multiple inputs without needing any special format. Here's how I've set up my parameter block: ```pwsh param ( [parameter(Position = 0, Mandatory = $true)] [ValidateSet("dev", "uat", "prd")] [String]$env, [parameter(Position = 1, Mandatory = $false)] [ValidateSet("blue", "green")] [String]$colour = "", [parameter(Mandatory = $false, ValueFromRemainingArguments = $true)] [String[]]$targets ) ``` The idea is that users can input targets as space-separated values like `-targets one two`. However, when I run the script without specifying the second optional parameter, I get an error related to the color validation. I've realized I could combine the environment and color parameters, but I'd really like the flexibility for users. Also, bonus points if it can handle formats like `this.might.be.one["too"]` easily. Any advice on how to structure this? Thanks!

Answers

rhubarb crumble
If you want the targets parameter to accept multiple values without needing them wrapped in a string, just restructure your parameters. Make sure `ValueFromRemainingArguments` is correctly applied. It could look something like this: ```pwsh param ( [String]$env, [String]$colour, [String[]]$targets,$_ ) ``` And you can pass `-targets one two` directly without needing additional squiggles! Just remember the order is crucial!

cinnamon+305: Thanks for that tip! I see the possibilities. I guess I was just overcomplicating it.

Reply to this answer
drumstick grip!
Whatโ€™s actually driving you to have these optional positional parameters? There could be simpler ways to structure your script or functions, maybe even a wrapper for the calls? Providing a bit more context on your overall goal could lead to better suggestions!

cinnamon+305: Great point! The script is meant to streamline local Terraform development across various environments, so I need to specify the correct environment and color easily. I want to keep it user-friendly.

Reply to this answer
not remotely close
It sounds like you might need to tweak how your -targets parameter is structured. Instead of just space-separated inputs, try using comma-separated values when you call the script, so like: `script.ps1 dev -targets one, two`. This way, it recognizes them as part of a single array input!

northern.pike: I gave that a shot, and it actually worked! But I'd prefer not to add extra commas if I can avoid it.

Reply to this answer
cap10nF
Maybe consider using a file for your targets instead? That could simplify things and reduce command line clutter, though I get it might add steps to your workflow if itโ€™s not needed.

cinnamon+305: Thatโ€™s a great idea, but it might slow me down. Just pasting text into the CLI feels quicker!

Reply to this answer
_turn the page
I'd suggest looking at argument mode in PowerShell. In this mode, you donโ€™t actually need to encapsulate parameters like `targets` in an array with `@(...)`. Just using commas will suffice, which simplifies your command a lot. For example: `script.ps1 dev -targets one, two` can be used instead, without the extra complexity.

..northbound: Totally agree! I didnโ€™t realize arrays didnโ€™t need that syntax. Super helpful!

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions