Force named parameters

by Jul 26, 2011


Hello everybody,

I'm writing a new
powershell script for some standard tasks that have to be done periodically. I want to use
named parameters which isn't a problem. The problem occurs when someone else
uses the script incorrectly.

To illustrate my problem I have made this sample
script (Arguments.ps1).








function Usage(){

      write "Usage: Scriptname     -User:<username> -Computer:<computername>



if($args.Count -ne 0){






if ($User -ne ""){

      Write-Host $User


if ($Computer -ne ""){

      Write-Host $Computer


if ($Usage -eq $true){



When is call this
script as follows:
Arguments -User:Sidney -Computer:MyPC -Usage
Arguments-Computer:MyPC -User:Sidney -Usage
Arguments -User:Sidney -Computer:MyPC test
Arguments -Computer:MyPC -User:Sidney -Test:test

the script executes
as I want it to. The correct parameters are being filled and test is seen as an
incorrect parameterargument. When I execute the script this way:
Arguments test -User:Sidney
test is being place in parameter $Computer. This is not what I want. Is there a way
to Force the use of named parameters?

Best Regards,

Sidney Mulder