1

I am attempting to make an array of arrays where there is an array of foldernames that points to an array of file paths:

Folder1: File1, File2, File3
Folder2: File1, File2, File3
etc...

The code I have come up with is:

#Paths of the folders being patched
$HF_Folders_To_Patch_LIST = Get-Childitem $HF_Source_Path | Where-Object {$_.PSIsContainer} | Foreach-Object {$_.FullName}
$HF_FILES_LIST = $HF_Folders_To_Patch_LIST | ForEach-Object { ,@(Get-ChildItem -Path $FolderPath | Foreach-Object {$_.FullName}) }

From my understanding I should be using "@()" or ",@()", however I can't seem to find too many resource on creating an array within an array online, I maybe googling it wrong. Am I on the right track to do it this way, or is it even possible to do it this way? I can make a for loop and probably get the outcome I want, but I feel as if I am misunderstanding how arrays work in powershell when using the pipes.

2
  • 1
    Please do not edit an answer into your question. You are free to post an answer of your own if you came up with your own solution. Commented Jul 23, 2018 at 23:58
  • Fixed it. Thank you for the advice. Commented Jul 24, 2018 at 13:54

2 Answers 2

1

The Scripting Guys TechNet Blogs has a rather good article about the basics for Arrays in Arrays and how to access the data through the position on the arrays.

You can use arraylists to easily add items to an array, the item you add to the array can be another array.

So lets create an arraylist:

$myMainArrayList = New-Object System.Collections.ArrayList

Now lets create a new arraylist that we can add to our main arraylist.

$mySubArrayList = New-Object System.Collections.ArrayList

You can now add items to your sublist:

$stringObject = "This is a string"
$mySubArrayList.add($stringObject)

Now you can add your subarray to your main array

$mySubArrayList.add($mySubArrayList)

You can add as many subarrays into your main array as you would like.

Sign up to request clarification or add additional context in comments.

1 Comment

Now that I looked at it, I see my mistake, I need to be using a hashtable, where I would want to make the folder name the Key and the Value an array of file paths. And the pipe would give me an unknown number of keys that have an unknown number of files per a key.
0

After looking at the answer below and realizing I need something like a hashmap, the powershell hashtable function was the answer here is the code that worked for me:

$HF_Folders_To_Patch_LIST = Get-Childitem $HF_Source_Path | Where-Object {$_.PSIsContainer} | Foreach-Object {$_.FullName}

$HF_FILES_LIST = @{}
$HF_Folders_To_Patch_LIST | ForEach-Object { $HF_FILES_LIST.Add($_, @(Get-ChildItem -Path $_ | Foreach-Object {$_.FullName})) }

Comments

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.