Reputation: 750
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
Upvotes: 1
Views: 247
Reputation: 403
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
Upvotes: 3