3

I have a requirement to request some information that comes in the form of an object. I need to replace some test in one of the properties and then write the list of objects to CSV.

When I do

Get-Process | select * | %{ $_Path.Replace("chrome", "ie") }

I have two problems

  1. If $_.Path is null, it gives me an error that you cannot call a method on a null-valued expression
  2. The output is a single string representing the text that was replaced (just the Path property). I need the original object and all of it's properties kept, but with the updated path value.

So of course when I try to do

Get-Process | select * | %{ $_Path.Replace("chrome", "ie") } | Export-Csv -Path "out.csv"

What I get is a single property Length because the output of the above is a string with only the Length property.

esac
  • 24,099
  • 38
  • 122
  • 179

2 Answers2

2
Get-Process | select * | %{ $_.Path = $_.Path.Replace("chrome", "ie"); $_ } | Export-Csv -Path "out.csv" -NoTypeInformation
  • $_.Path instead of $_Path
  • Assign the replaced text back to the path property
  • and output the object after doing the assignment

Help Links (if available):

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
2

try this

 Get-Process | select *, @{N="Path";E={$_.Path.Replace("chrome", "ie") }} -ExcludeProperty Path | export-csv -Path "c:\temp\out.csv" -NoTypeInformation
Esperento57
  • 16,521
  • 3
  • 39
  • 45