How do select a property in a file when pipelined by get-content?

Welcome Forums General PowerShell Q&A How do select a property in a file when pipelined by get-content?

  • This topic has 5 replies, 4 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 5 reply threads
  • Author
    Posts
    • #249902
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      Hello there, I am a newbie with Powershell.

      I have got a file which was printed out to a .txt file from a cmd in PowerShell (eg: Get-Process)

      Get-Process | Out-File -FilePath “C:\temp\processes.txt”

      After that, I pipeline it via the Get-Content cmdlet and I want to select a column to continuously pipeline to Where-Object cmdlet (eg: Process name). So how can I use $_.Processname ?

      Get-Content -Path “C:\temp\processes.txt” | Where-Object {$_.Processname -match “svchost”}

      Thank you!

       

    • #249914
      Senior Moderator
      Topics: 9
      Replies: 1309
      Points: 4,781
      Helping Hand
      Rank: Community Hero

      Once the output is redirected to a file , it becomes string/plain text. PowerShell pipelines to connect cmdlets are basically to handle objects, so this way it wont be possible. But if you want to store the output as file then Export-CliXml is the hero here. It creates xml file which preserves the object with its type along with the output.

    • #250061
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      Once the output is redirected to a file , it becomes string/plain text. PowerShell pipelines to connect cmdlets are basically to handle objects, so this way it wont be possible. But if you want to store the output as file then Export-CliXml is the hero here. It creates xml file which preserves the object with its type along with the output.

      Thank you a lot πŸ™‚ however, there was a time I saw someone using a txt file which was self-created by him with a header as property and other values below it.

      He also pipelines it by get-content and something else after to handle it. Can you consider it? πŸ™‚

       

    • #250100
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      header as property and other values below it.

      It sounds like you are describing a CSV file. See if this is what you’re after.

      Get-Process | Export-CSV c:\temp\processes.txt -NoType

      and then

      Import-CSV c:\temp\processes.txt | where processname -match "svchost"

    • #250340
      Participant
      Topics: 51
      Replies: 209
      Points: 860
      Helping Hand
      Rank: Major Contributor
      • This reply was modified 1 month ago by Iain.
    • #250820
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      header as property and other values below it.

      It sounds like you are describing a CSV file. See if this is what you’re after.

      Get-Process | Export-CSV c:\temp\processes.txt -NoType

      and then

      Import-CSV c:\temp\processes.txt | where processname -match "svchost"

      Great !!! That is exactly what I was looking for πŸ˜€

      Thank you,

Viewing 5 reply threads
  • You must be logged in to reply to this topic.