1

I need to declare an array parameter in an called PS script. The array coming in from the caller/source script is in a hash table:

$Array = @(1, 2, 3)
$myHashTable = @{myArr = $Array}

Call: Z:\called.ps1 $myHashTable

In 'called.ps1', what is the difference between

Param(
$myArr
)

,

Param(
[array] $myArr
)

and

Param(
[array[]] $myArr
)

?

1
  • the 1st will accept any object type. the 2nd will accept most any type, but convert it to an array if not already one. the 3rd accepts most any type - and will convert it to a two-dimensional array. Commented Sep 22, 2019 at 20:20

1 Answer 1

1

You can test that fairly directly. [grin] your examples give ...

  • any type = remains that type
  • 1d array = converts to an array if not one already
  • 2d array = converts to a jagged array (array of arrays) if not one already

demo code ...

function Test-Parameter
    {
    Param
        (
        $GenericVar,
        [array]$OneD_Array,
        [array[]]$TwoD_Array
        )

    $GenericVar.GetType()
    $OneD_Array.GetType()
    $TwoD_Array.GetType()
    }

Test-Parameter -GenericVar 'One' -OneD_Array 'Two' -TwoD_Array 'Three'

output ...

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
True     True     Object[]                                 System.Array
True     True     Array[]                                  System.Array
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.