ca9163d9
ca9163d9

Reputation: 29159

Add additional properties for "select-object"?

I try to add additional properties to a select-object output. However, it got the following error?

Select-Object : A positional parameter cannot be found that accepts argument 'System.Object[]'.

$c = @{a = "...","...","..."; b = "...","...","..."}
$files | 
% {
    $filepath = $_.fullname
    write-host $filepath
    $type = getType $_.name

    # The following statement works
    Import-Csv $_ | select $c[$type] | OUT-GRIDVIEW

    # The following doesn't work 
    Import-Csv $_ | select $c[$type] -property @{n="File"; e={$filepath}}| out-gridview
}

Upvotes: 0

Views: 1041

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

You're trying to use -Property twice, implicitly with the positional parameter $c[$type] and explicitly with your calculated property. You need to combine your property list and the calculated property to a single array and pass that to the -Property parameter.

One way to do this is to use the expression @PetSerAl suggested in the comment to your question:

$files | % {
  $filepath = $_.fullname
  $type = getType $_.name

  Import-Csv $_ |
    select @($c[$type]; @{n="File";e={$filepath}}) |
    Out-GridView
}

However, I think it would be simpler to just include the calculated property in your original property list(s):

$c = @{
  a = "...", "...", "...", @{n='File';e={$_.FullName}}
  b = "...", "...", "...", @{n='File';e={$_.FullName}}
}

so you don't need to manipulate that list later on:

$files | % {
  $type = getType $_.Name
  Import-Csv $_ | select $c[$type] | Out-GridView
}

Upvotes: 1

Related Questions