1

I want to be able to have multiple forms of the same parameter like so:

param(
  [string]$p or $path = "C:\",
  [string]$f or $filter = "*.txt",
  [switch]$o or $overwrite
)

but I'm not sure how to do this. Most times, you would only be able to choose one (e.g. only $p or only $path). Is it possible to use multiple names for the same variable/parameter?

2 Answers 2

3

Like this:

param(
  [Alias('p')]
  [string]$path = "C:\",
  [Alias('f')]
  [string]$filter = "*.txt",
  [Alias('o')]
  [switch]$overwrite
)

Note you can have multiple aliases too: [Alias('p','thepath')]

Sign up to request clarification or add additional context in comments.

Comments

3

PowerShell partial parameter name matching may be what your looking for.

# test.ps1
param($path)
write-host $path

Calling .\test.ps1 with either .\test.ps1 -path "c:\windows" or .\test.ps1 -p "c:\windows" will both match, and populate, the $path parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.