1

I am new to Powershell and I am still discovering -

[string[][]] $adusers = (
     ( "John", "Smith", "jsmith", "jsmith@smitty.com" ),
     ( "James", "Johnson", "jjohnson", "jjohnson@smitty.com" )
)

The second dimension can be returned as 1 dimension array.

 $adusers.GetType();
 $adusers[0].GetType();
 $adusers[1][2].GetType();

 IsPublic IsSerial Name                                     BaseType                                                                                 
 -------- -------- ----                                     --------                                                                                 
 True     True     String[][]                               System.Array                                                                             
 True     True     String[]                                 System.Array                                                                             
 True     True     String                                   System.Object                                                                            

For Each with a 2 Dimensional Array concatenates the array elements of the 2nd dimension with a space delimiter and returns a string object.

Is there a syntax I am missing to CAST the $user variable to a dimension? Should I have to?

Why does powershell concatenate them all together as a String?

$ForEach($user in $adusers){

    $adusers.GetType();
    $user.GetType();

    $user[0]
    $user[1]
    $user[2]
    $user[3]

}

Output

IsPublic IsSerial Name                                     BaseType                                                                                 
-------- -------- ----                                     --------                                                                                 
True     True     String[][]                               System.Array                                                                             
True     True     String                                   System.Object                                                                            
J
o
h
n
True     True     String[][]                               System.Array                                                                             
True     True     String                                   System.Object                                                                            
J
a
m
e

**************************** UPDATE ******************************

The problem ends up being that I had initialized the $user variable earlier in a different fragment of code in the same script (using run selection).

[String]$user = "testUser"

The ForEach was casting the 1 dimension array to a string - assuming that is what I wanted.

I tried the solution of declaring $user as a 1 dimension array but incorrectly:

[string]$user = ""
[string[]]$user
$user.GetType();

IsPublic IsSerial Name                                     BaseType                                                                                 
-------- -------- ----                                     --------                                                                                 
True     True     String                                   System.Object                                                                            

What I needed to do is declare $user as a 1 dimension array and initialize it with a 1 dimension array:

[string]$user = ""
[string[]]$user = ("")
$user.GetType();

IsPublic IsSerial Name                                     BaseType                                                                                 
-------- -------- ----                                     --------                                                                                 
True     True     String[]                                 System.Array   

Or start a new Powershell Session where $user has not been previously declared and instantiated. That also worked.

Like I said I'm new to Powershell and still learning.

I also spent some time in this Question and Answers to get some insight regarding how to clear variables in the shell - interesting.

powershell - Remove all variables

Threadid
  • 730
  • 1
  • 7
  • 27
  • Does this answer your question? [Powershell Multidimensional Arrays](https://stackoverflow.com/questions/9397137/powershell-multidimensional-arrays) and/or [Why does Powershell combines array of arrays?](https://stackoverflow.com/q/36620054/1701026) – iRon Mar 08 '20 at 11:05
  • Does this answer your question? [Powershell Multidimensional Arrays](https://stackoverflow.com/questions/9397137/powershell-multidimensional-arrays) – Theo Mar 08 '20 at 11:08
  • Thanks for the suggestion. This post is very similar to mine and led me to finding the answer: https://stackoverflow.com/questions/18282360/powershell-two-dimension-arrays – Threadid Mar 08 '20 at 14:39

1 Answers1

3

I am sorry, that I cannot reproduce your error, as my Output is without any splits.

[string[][]] $adusers = (
     ( "John", "Smith", "jsmith", "jsmith@smitty.com" ),
     ( "James", "Johnson", "jjohnson", "jjohnson@smitty.com" )
)

ForEach ($user in $adusers){

    $adusers.GetType();
    $user.GetType();

    $user[0]
    $user[1]
    $user[2]
    $user[3]

}

IsPublic IsSerial Name                                     BaseType                                                                                                      
-------- -------- ----                                     --------                                                                                                      
True     True     String[][]                               System.Array                                                                                                  
True     True     String[]                                 System.Array                                                                                                  
John
Smith
jsmith
jsmith@smitty.com
True     True     String[][]                               System.Array                                                                                                  
True     True     String[]                                 System.Array                                                                                                  
James
Johnson
jjohnson
jjohnson@smitty.com

PS C:\Users\username> Get-Host

Name             : Windows PowerShell ISE Host
Version          : 5.1.17134.858
InstanceId       : 4c82dfcb-cdf7-4eb5-8c0a-25d2ad359ba3
UI               :   System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : de-DE
CurrentUICulture : de-DE
PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
FunnyO
  • 383
  • 2
  • 20
  • 3
    Thanks for second set of eyes. This confirmed my expectation that it should in fact be working as expected. A similar example from this question runs fine in the same shell. So I changed my focus the the $user variable. This was declared earlier as [string] and ForEach was casting from array to string. https://stackoverflow.com/questions/18282360/powershell-two-dimension-arrays – Threadid Mar 08 '20 at 14:43
  • I honor the explaination of the problem you had, as only few people post their actual result, when they discover the error. – FunnyO Mar 08 '20 at 16:53