PowerShell passing of variables (param)


At the beginning of a PowerShell script parameters can be defined in the function Param().

Param - passing variables to a script.

Variables inside "param" can be passed to the script when the script is called.

param (

A default value can be assigned in the param block, which is overwritten with another value when the script is called and the parameter is specified:

File types

Which file types are allowed as parameters is defined with [datatype]$variablename. A list of the possible data types can be found here: PowerShell variables and data types

If "switch" is used as data type, no values must be specified for the parameters:


With the addition [CmdletBinding()] PowerShell scripts or functions are converted into cmdlets. PowerShell attaches additional parameters to the function:

Powershell ISE provides a snippet template for this: cmdlet (advanced function), see also: Powershell ISE - Snippet


Variables with the addition Mandatory are mandatory for execution

Parameters with multiple values

By separating the values with a comma "," multiple variables can be passed. These can be handled in a foreach loop

function folders

        foreach ($Folder in $Folders){
            write-host "folder:$($Folder)"


PS C:\temp> .\parameter.ps1
PS C:\temp>

Parameters without value: switch

write-host $Option
write-host $2ndOption

When called without parameters, the two optional parameters are returned with $false:

PS C:\temp> .\parameter.ps1
PS C:\temp>

if the options are specified in the call, the switch parameter is set to $true:

PS C:\temp> .\parameter.ps1 -Option
PS C:\temp>

Read the possible parameters

PS C:\temp> (get-command .\parameter.ps1).parameters

Key                 Value                                         
---                 -----                                         
Option              System.Management.Automation.ParameterMetadata
2ndOption           System.Management.Automation.ParameterMetadata
Verbose             System.Management.Automation.ParameterMetadata
Debug               System.Management.Automation.ParameterMetadata
ErrorAction         System.Management.Automation.ParameterMetadata
WarningAction       System.Management.Automation.ParameterMetadata
InformationAction   System.Management.Automation.ParameterMetadata
ErrorVariable       System.Management.Automation.ParameterMetadata
WarningVariable     System.Management.Automation.ParameterMetadata
InformationVariable System.Management.Automation.ParameterMetadata
OutVariable         System.Management.Automation.ParameterMetadata
OutBuffer           System.Management.Automation.ParameterMetadata
PipelineVariable    System.Management.Automation.ParameterMetadata
positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Questions / Comments