Collin Stump Collin Stump - 3 months ago 8
PowerShell Question

Difference between [datatype] and [datatype[]]

When casting a datatype for a variable, what does the extra empty square brackets do?

For example, in defining a parameter for a function, I have noticed these two ways of casting a data type for that parameter:

param(
[string]$computername
)


vs:

param(
[string[]]$computername
)


What purpose does the empty square brackets in the second example serve?

Answer

The parameter in the second example accepts a list of strings (an array), the parameter in the first example only a single string.

This works in both cases:

-computername 'foo'

This works just in the second case:

-computername 'foo', 'bar'