$cars = "bmw","audi","volvo","vw"
echo $cars.length
returns 4, but
$cars = "bmw"
returns 3 because it counts the characters..
Is there a way I can return 1 if the array only contains one item?
See Question&Answers more detail:os$cars = "bmw","audi","volvo","vw"
echo $cars.length
returns 4, but
$cars = "bmw"
returns 3 because it counts the characters..
Is there a way I can return 1 if the array only contains one item?
See Question&Answers more detail:osA couple other options:
Use the comma operator to create an array:
$cars = ,"bmw"
$cars.GetType().FullName
# Outputs: System.Object[]
Use array subexpression syntax:
$cars = @("bmw")
$cars.GetType().FullName
# Outputs: System.Object[]
If you don't want an object array you can downcast to the type you want e.g. a string array.
[string[]] $cars = ,"bmw"
[string[]] $cars = @("bmw")