Author Posts

April 23, 2018 at 2:34 pm

I'm just getting started, and trying to pass command-line parameters to a PowerShell script.

After checking the documentation, I thought something like this would work:

Param ( String[] $PackageId, String[] $NOGDOC )

However, after attempting it, the error messages seem to suggest that this method is for passing parameters to internal functions, rather than from the (external) command line.

Are the command-line arguments available in a pre-defined variable?

Thanks in advance for any help you can provide.

April 23, 2018 at 2:43 pm

It's a syntax issue.

Example: (test.ps1)

Param(
    [string] $Param1,
    [int] $Param2,
    [string[]] $Param3
)

Write-Host "$Param1 : $Param2"
Write-Host "Array count: $($Param3.count)"
$Param3 | ForEach-Object {Write-Host "ArrayValue: $_"}

Usage:

.\test.ps1 -Param1 "A String" -Param2 7 -Param3 "Value1", "Value2", "Value3"

Results

A String : 7
Array count: 3
ArrayValue: Value1
ArrayValue: Value2
ArrayValue: Value3