Powershell: what kind of data type is [string[]] and when would you use it?
Posted by jpluimers on 2019/08/13
[WayBack] In Powershell, what kind of data type is [string[]] and when would you use it? (thanks cignul9 and arco444!): basically it forces an array of string.
It defines an array of strings. Consider the following ways of initialising an array:
[PS] > [string[]]$s1 = "foo","bar","one","two",3,4 [PS] > $s2 = "foo","bar","one","two",3,4 [PS] > $s1.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String[] System.Array [PS] > $s2.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.ArrayBy default, a powershell array is an array of objects that it will cast to a particular type if necessary. Look at how it’s decided what types the 5th element of each of these are:
[PS] > $s1[4].gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object [PS] > $s2[4].gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Int32 System.ValueType [PS] > $s1[4] 3 [PS] > $s2[4] 3The use of
[string[]]when creating$s1has meant that a raw3passed to the array has been converted to aStringtype in contrast to anInt32when stored in anObjectarray.
–jeroen






Leave a comment