Using ForEach with Get-ChildItem -recurse

Viewed 109183

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?

3 Answers

I was cleaning up some profiles just a moment ago and found this post during my process and thought I would share how I looped through the results of get-childitem(ls is just an alias of gci I think?). Hope this helps someone somewhere.

$blob = (ls).name 
foreach ($name in $blob) { 
get-childitem "D:\CtxProfiles\$name\Win2012R2v4\UPM_Profile\AppData\Local\Google\Chrome\User Data\Default\Default\Media Cache\f_*" -erroraction 'silentlycontinue'|remove-item -force -recurse -confirm:$false 
}
Related