Passing named arguments in windows powershell scripting

Create a simple script “s1.ps1” with below content. We can also pass the named arguments to the script using below syntax. Create a simple script – “s1.ps1” with below code.
 
Param(
    [string]$name,
    [string]$Id
    )
    
    echo “Your name is $name”
    
    echo “Your Id is $Id”

Now you can invoke above script as specified below. Note that all of the invocations are valid.
 
./s1.ps1 –Name Paul –Id 1234
./s1.ps1 –Id 1234 –Name Paul
./s1.ps1 Paul 1234

More advanced Parameters

In above example, parameters were optional. But some times, we want mandatory parameters. In below example, we have made both the parameters as mandatory and first parameter is positional. If you do not specify the parameter, Powershell will ask you for it.
 
Param(
    [Parameter(Mandatory=$True,Position=1)]
    [string]$Name,
    
    [Parameter(Mandatory=$True)]
    [string]$Id
    )
    
    echo “Your name is $Name”
    
    echo “Your name is $Id”

Web development and Automation testing

solutions delivered!!