user3342256
user3342256

Reputation: 1290

Inserting Delimiter into PowerShell Output

The result of Get-Process -Name explorer | select Handles,WS,CPU,Id,ProcessName | ft -HideTableHeaders returns the following output:

2623 255336448 178.125 10080 explorer 

In order to ingest this data into a third party system, I need to pipe delimit the result as such:

2623|255336448|178.125|10080|explorer 

What is the best way to achieve this?

Upvotes: 3

Views: 7507

Answers (2)

mklement0
mklement0

Reputation: 437998

To offer a more concise (and slightly faster) alternative to Bill Stewart's helpful answer:

Get-Process -Name explorer | ForEach-Object { 
  $(foreach ($prop in 'Handles', 'WS', 'CPU', 'Id', 'ProcessName') { $_.$prop }) -join '|'
}
  • foreach ($prop in 'Handles', 'WS', 'CPU', 'Id', 'ProcessName') { $_.$prop } outputs all the properties of interest for each process object ($_, the input object at hand provided by the ForEach-Object cmdlet).

  • $(...) collects them as an [object[]] array, which ...

  • ... enables that array's use as the LHS of the -join operator in order to join the array's elements with | as the separator to form a single string.

Overall, you get a single string per input object, as desired.

Upvotes: 1

Bill_Stewart
Bill_Stewart

Reputation: 24555

How about:

(Get-Process explorer |
  Select-Object Handles,Ws,CPU,ID,ProcessName |
  ConvertTo-Csv -Delimiter '|' -NoTypeInformation |
  Select-Object -Skip 1) -replace '"',''

Only use ft (Format-Table) for easy viewing in the PowerShell console (it's not good for sending data to other applications, because then you would have to undo the formatting - so don't format in the first place).

Upvotes: 5

Related Questions