1

I am new to Powershell and I am still discovering -

[string[][]] $adusers = (
     ( "John", "Smith", "jsmith", "[email protected]" ),
     ( "James", "Johnson", "jjohnson", "[email protected]" )
)

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

3

1 Answer 1

3

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

[string[][]] $adusers = (
     ( "John", "Smith", "jsmith", "[email protected]" ),
     ( "James", "Johnson", "jjohnson", "[email protected]" )
)

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
[email protected]
True     True     String[][]                               System.Array                                                                                                  
True     True     String[]                                 System.Array                                                                                                  
James
Johnson
jjohnson
[email protected]

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
Sign up to request clarification or add additional context in comments.

2 Comments

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. stackoverflow.com/questions/18282360/…
I honor the explaination of the problem you had, as only few people post their actual result, when they discover the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.