Count length of array and return 1 if it only contains one element Count length of array and return 1 if it only contains one element powershell powershell

Count length of array and return 1 if it only contains one element


A couple other options:

  1. Use the comma operator to create an array:

    $cars = ,"bmw"$cars.GetType().FullName# Outputs: System.Object[]
  2. 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")


Instead of writing echo $cars.length write echo @($cars).length


declare you array as:

$car = array("bmw")

EDIT

now with powershell syntax:)

$car = [array]"bmw"