user1612851
user1612851

Reputation: 1214

Outputting Array of objects - all come on one line

I am trying to list one property of an object for each element in an array. It is fine, but they all show up on one line. I read to use $OFS to set the separation characters, but that didn't work either:

$filearray =@()
foreach($file in $files)
{
    # There is more logic here to determine what files are included
    $filearray = $filearray + $file  
}
$OFS = "`r`n"
$filearray | Select Fullname

Upvotes: 0

Views: 1044

Answers (5)

Keith Hill
Keith Hill

Reputation: 201662

The way to use $OFS is set it to CRLF like you have then evaluate an array variable in a double quoted string like so "$filearray". If it is still show up on one line then check the type of $filearray as @mjolinor suggests in his comments on his answer.

Upvotes: 1

CB.
CB.

Reputation: 60918

Try to force type at this line:

[object[]]$filearray = $filearray + $file 

or better

[object[]]$filearray += $file 

If you know the rigth type ( [fileinfo[]] maybe? ) you can be more accurate forcing the rigth type array

Upvotes: 0

mjolinor
mjolinor

Reputation: 68273

I'd explicitly cast $filearray to make sure it's not getting reset somewhere else in the code:

[array]$filearray = @()

Upvotes: 0

Mike Shepard
Mike Shepard

Reputation: 18156

Why not something like this:

$filecollection | where-object  { #condition } | select FullName

Upvotes: 0

Christopher Douglas
Christopher Douglas

Reputation: 1550

I would try something like this:

Foreach($file in $FileCollection)
{
 #Logic
  if($file #meets condition)
  {
    $filearray = $filearray + $file.Property
  }

}

That will load the array with the property value, if you know the name.

Upvotes: 0

Related Questions