0

I’m pushing data from Get-ChildItem to a csv, via some Where-Object and Select-Object directives as below.

The problem is that the first line of the csv contains the Select clause itself (eg literally { “This part” + $_.AndThisBit }. Not sure why part of my code is leaking into the data?

Get-ChildItem "E:\Some\Path" -Recurse -Include *.msg, *.eml | 
    Where-Object { $_.Name.StartsWith("Confidential") -eq $false } |
    Select-Object { "Fixed-prefix*" + $_.FullName } |
    Export-Csv -Path "C:\another\path\Results.csv" -NoTypeInformation
Clijsters
  • 4,031
  • 1
  • 27
  • 37
sasfrog
  • 2,410
  • 1
  • 17
  • 28
  • 1
    As a rule of thumb, avoid excessive piping. It would be much easier to debug the code if there are intermediate variables to inspect. – vonPryz Sep 08 '18 at 07:30
  • 1
    @vonPryz, for debugging this might be true, but not for a general rule of thumb for e.g. production scripts. Assigning a stream to a variable will choke the pipeline and could impact the performance. – iRon Sep 08 '18 at 19:34

1 Answers1

3

Select-Object doesn't take ScriptBlocks, as Where-Object does. What you might want is:

Select-Object -Property @{Name="Prefix"; Expression={"Fixed-prefix*" + $_.FullName}}

To define a custom property with an Expression. That Expression is a ScriptBlock and gets evaluated.

Clijsters
  • 4,031
  • 1
  • 27
  • 37