Using ForEach with Get-ChildItem -recurse

powershell

Solution

I don't know why you're trying to step through formatted data at all. But as it is, `$table` is just a collection of strings. So you can do the following:

$table = get-childitem -recurse | where {! $_.PSIsContainer} | Format-Table Name, Length

foreach ($row in $table)
{
  $row
}

But I don't know why you'd want to. If you're trying to do something with the data in the file you could try this:

$files = get-childitem -recurse | where {! $_.PSIsContainer}
foreach ($file in $files)
{
    $file.Name
    $file.length
}

Problem

I am trying to get a recursive list of files in a particular subfolder structure, then save them to a table so that I can use a foreach loop to work with each row. I have the following code: ``` $table = get-childitem -recurse | where {! $_.PSIsContainer} | Format-Table Name, Length foreach ($row in $table) { $row[0] $row[1] } ``` If I try to output `$table` as-is, it looks perfect, with two columns of data for all of the files. If I try and step through with foreach (like above), I get the `"Unable to index into an object of type Microsoft.PowerShell.Commands.Internal.Format.FormatEndData."` error message. What am I doing wrong?

Original source