Passing and handling parameter in Windows PowerShell script

Like in any other language, in PowerShell we also want to reuse our code and execute same set of operations with different parameters. PowerShell has great support for parameters which can be used in PowerShell script invocation.

If you want your script to accept parameters you need to declare them in the script file. The following is parameter declaration sample and it contains few options which will be explained.

Param( [Parameter(Mandatory=$True, Position=1)] [string]$logFolder, [Parameter(Mandatory=$True)] [int]$fileAge, [switch]$deleteFile ) Write-Output $logFolder Write-Output $fileAge Write-Output $deleteFile

Now let's explain the keywords from the sample above

Mandatory - Script will not execute unless parameter value is passed

- Script will not execute unless parameter value is passed Position - If you do not specify the parameter name in the script call, this is the index of the parameter from which value will be taken

- If you do not specify the parameter name in the script call, this is the index of the parameter from which value will be taken [string],[int],[switch] - Data type of the parameter. Data types will be explain in this article.

Switch parameter type

Let's invoke the script from above and see the output

C:\> powershell.exe -command test.ps1 -logFolder c:\logs\ -fileAge 30



c:\logs\

30

IsPresent

---------

False

You will notice that for the last parameter you have the following output

IsPresent

---------

False

This is the purpose of switch parameter type. It returns false if the parameter is present or not. If you change the previous call to the following you will get the different output

C:\> powershell.exe -command test.ps1 -logFolder c:\logs\ -fileAge 30



IsPresent

---------

True

Position attribute

This attribute tells powershell that you are exception this parameter at specific position and it will be fetched even if you do not declare the parameter name in the script file invocation

C:\> powershell.exe -file d:\temp\params.ps1 c:\logs\ -fileAge 30 -deleteFile

Parameter types

Invoking powershell script file supports pretty much all types of data you can use for powershell code functions parameters

[int] 32-bit signed integer [long] 64-bit signed integer [string] Fixed-length string of Unicode characters [char] Unicode 16-bit character [bool] True/false value [byte] 8-bit unsigned integer [double] Double-precision 64-bit floating point number [decimal] 128-bit decimal value [single] Single-precision 32-bit floating point number [array] Array of values [xml] Xmldocument object [hashtable] Hashtable object (similar to a Dictionary object)

Invoking script with simple types is trivial and does not need any special explanation. The tough part is invoking script with complex types. These are types marked with blue color in previous list. So let's start with them one by one.

Array type parameters

Param( [Parameter(Mandatory=$True)] [String[]] $fruits ) Write-Output $fruits.Count foreach($fruit in $fruits){ Write-Output $fruit }

C:\> powershell.exe -Command c:\temp\params-array.ps1 -fruits Banana,Kiwi,Orange



3

Banana

Kiwi

Orange

Xml type parameters

Note Moe efficient way to deal with XML is to pass the path and then open the document inside the PowerShell script rather than sending whole document as a parameter which may lead to issues because of the XML document complexity

Param( [Parameter(Mandatory=$True, Position=1)] [xml]$xmlDoc ) foreach($fruit in $xmlDoc.SelectNodes("/fruits/fruit")){ Write-Output $fruit.InnerXml }

C:\> powershell.exe -command c:\temp\param-xml.ps1 -xmlDoc "'<fruits><fruit>Banana</fruit><fruit>Kiwi</fruit><fruit>Orange</fruit></fruits>'"



Banana

Kiwi

Orange

Hashtable type parameters

Param( [Parameter(Mandatory=$True)] [hashtable]$myObject ) Write-Output $myObject.FirstName Write-Output $myObject.LastName Write-Output $myObject.Email

C:\> powershell.exe -Command c:\temp\params-object.ps1 -myObject "@{FirstName='Dejan';LastName='Stojanovic';Email='sample@email.com'}"



Dejan

Stojanovic

sample@email.com

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.