If I define the following
[parameter(Mandatory = $true)]
[alias("db")]
[string]$database,
then I get an error
Parameter alias cannot be specified because an alias with the name 'db'
was defined multiple times for the command.
Which is true, since db is already an alias for the universal -Debug parameter.
Is it possible to define this alias without renaming the parameter?
Sorry, you can’t.
-Debugis a common parameter, so-Debugand-dbare switches that are available on pretty much everything including the functions you write yourself. As the error tells you, it’s already defined.Even if it were possible to go around undefining built-in aliases, that unexpectantly changes the meaning of a call like
test-db -dbfor someone else who frequently uses-dbinstead of-Debug. They expect it to enable debugging output, not specify a different parameter.Consider this function:
Now call it with
test-db server,test-db -db server, andtest-db server -db. The first doesn’t dowrite-debug, while the other 2 do, no matter where-dbis. You also can’t define a separate parameter[string]$db(or rename$databaseto$db) because Powershell will give you this error:More info on this, per MSDN (updated since old link died):