Powershell Printout only certain section of lines

powershell print variable to console
powershell write to file
powershell display output
powershell write-output to file
powershell output to window
powershell write-output variable and text
powershell write-output no newline
powershell write-host

I see a lot of "grab last word", "grab first/last X characters" or "grab lines that contain XXX" however I've been unable to find a solution here for my problem. For example, my text file contains line like

[0000001] item_title apple_juice_2L_concentrate 0x000581
[0000002] item_description 98%_reconstituted_apple_juice 0x00585
[0000003] item_title orange_juice_2L_natural 0x000679
[0000004] item_description 100%_fresh_squeeze_orange_juice 0x00985
[0000005] item_title apple_juice_1L_natural 0x000429
[0000006] item_description 100%_natural_apple_juice_cloudy 0x00587

how would I achieve a printout like this while cutting out the junk i dont want like the [0000001] and the 0x000581

apple_juice_2L_concentrate
apple_juice_1L_natural

I'm currently using:

Get-Content -Path H:\"inventory management"\juice\juices.txt | Where-Object {$_ -like '*item_title apple_juice*'}

which returns

[0000001] item_title apple_juice_2L_concentrate 0x000581
[0000005] item_title apple_juice_1L_natural 0x000429

Appreciate your time!

Pretty picture for reference. https://imgur.com/fSRaH4R.png

Answered own question with assistance from other answers here.


this uses the .Split() string method to split on the spaces, then grabs the 3rd item [index = 2 since arrays start @ 0]. finally, it puts that into a $Results collection. you can use that whole collection OR filter it with $Results -match 'apple' to get the items that have apple in them.

# fake reading in a text file
#    in real life, use Get-Content
$InStuff = @'
[0000001] item_title apple_juice_2L_concentrate 0x000581
[0000002] item_description 98%_reconstituted_apple_juice 0x00585
[0000003] item_title orange_juice_2L_natural 0x000679
[0000004] item_description 100%_natural_apple_juice_cloudy 0x00985
[0000005] item_title apple_juice_1L_natural 0x000429
[0000006] item_description 100%_natural_apple_juice_cloudy 0x00585
'@ -split [System.Environment]::NewLine

$TargetPhrase = 'item_title'

$Results = $InStuff -match $TargetPhrase |
    ForEach-Object {
        $_.Split(' ')[2]
        }

$Results
'=' * 20
# if you want only the apple items
$Results -match 'apple'

output ...

apple_juice_2L_concentrate
orange_juice_2L_natural
apple_juice_1L_natural
====================
apple_juice_2L_concentrate
apple_juice_1L_natural

here's an alternate way that does exactly the same thing but uses part of the OPs code since he refuses to abandon that awkward, wrong-headed method. [grin] this presumes the file has been loaded with Get-Content into the $InStuff variable. if you really want things smashed into one line ... you can do that rather silly thing if you wish.

$TargetPhrase = 'item_title apple'
$InStuff |
    Where-Object {
        $_ -match $TargetPhrase
        } |
    ForEach-Object {
        $_.Split(' ')[2]
        }

output ...

apple_juice_2L_concentrate
apple_juice_1L_natural

Windows Powershell for Developers, CHAPTER5 Add PowerShell to Your GUI Adding scripting support to your integrators can customize the application's logic to better match their specific needs. it is to create the PowerShell engine, call some cmdlets, and print out the results. the following line into the <ItemGroup> section: <Reference Include​="System. Powershell Printout only certain section of lines all i want to do is change my existing code so it grabs the 3rd section only, not the entire line. everything


For reference, the answer that should've been provided:

$InStuff = Get-Content -Path 'H:\inventory management\juice\juices.txt' 

$TargetPhrase = 'item_title'

$Results = $InStuff -match $TargetPhrase |
    ForEach-Object {
        $_.Split(' ')[2]
        }

$Results -match 'apple_juice'

Windows PowerShell Step by Step: Window PowerS Step Step _p3, It makes working with some alphabetic and numeric characters easy. On the first line, declare a variable called $Query. You will select only the startname property and the name property from the Win32_Service WMI class You first format the list by the name property from Win32_Service and then print out the startName. Here’s how to print out only the specific areas of the spreadsheet that you need. Note: For this article, I am using Excel in Office 2013 , but the principle is the same in other versions of


I propose you a solution :

Get-Content "c:\temp\test.txt" | 
    ConvertFrom-Csv -Delimiter ' ' -Header ID, RowType, Libelle, Reference  | 
        where {$_.RowType -eq 'item_title' -AND $_.Libelle -match 'apple_juice'} | select Libelle

Write-Output, This switch only works correctly with PowerShell Core 6.2 and newer. On older versions of PowerShell Core, the collection is still enumerated even with use of  Get-Content -Path .\LineNumbers.txt -TotalCount 5This is Line 1This is Line 2This is Line 3This is Line 4This is Line 5. Example 3: Get a specific line of content from a text file. This command gets a specific number of lines from a file and then displays only the last line ofthat content.


Just to offer a different perspective using Select-String:

(Select-String -Path 'H:\inventory management\juice\juices.txt' -pattern "(?<=item_title\s*)apple_juice[^\s]*").Matches.Value

This could be modified to allow variables:

$category = 'item_title'
$juice = 'apple_juice'
(Select-String -Path 'H:\inventory management\juice\juices.txt' -pattern "(?<=$category\s*)$juice[^\s]*").Matches.Value

6 the most basic Powershell output commands, Write-Verbose is a specific command very important during writing Powershell scripts and functions. It's used when you want to print more details in result from  PowerShell is an interactive Command-Line Interface (CLI) and automation engine designed by Microsoft to help design system configurations and automate administrative tasks. This tool has its own command-line with a unique programming language similar to Perl.


Is your goal to get the item title from first line and item description from line after it? If that's what you want to do, here's an example:

# Sample data; replace this with Get-Content, etc.
$inputData = @"
[0000001] item_title apple_juice_2L_concentrate 0x000581
[0000002] item_description 98%_reconstituted_apple_juice 0x00585
[0000003] item_title orange_juice_2L_natural 0x000679
[0000004] item_description 100%_fresh_squeeze_orange_juice 0x00985
[0000005] item_title apple_juice_1L_natural 0x000429
[0000006] item_description 100%_natural_apple_juice_cloudy 0x00587
"@ -split [Environment]::NewLine

for ( $i = 0; $i -lt $inputData.Count; $i++ ) {
  if ( $inputData[$i] -match ' item_description ' ) {
    [PSCustomObject] @{
    "item_title"       = [Regex]::Match($inputData[$i - 1],' item_title (.+) 0x').Groups[1].Value
    "item_description" = [Regex]::Match($inputData[$i],' item_description (.+) 0x').Groups[1].Value
    }
  }
}

Output is:

item_title                 item_description
----------                 ----------------
apple_juice_2L_concentrate 98%_reconstituted_apple_juice
orange_juice_2L_natural    100%_fresh_squeeze_orange_juice
apple_juice_1L_natural     100%_natural_apple_juice_cloudy

This pattern assumes that there's a line in the input that contains the string item_description and that the line before it contains the string item_title.

How to read a text file in PowerShell quickly and easily, Many times, you may only have to read a certain part of a file to get the line and will print out each line with its line number for easy reading. Summary: The Scripting Wife learns how to use Windows PowerShell to get specific lines from a text file.. Microsoft Scripting Guy, Ed Wilson, here. As the Windows PowerShell 2011 Scripting Games edge forward in the conscious minds of intrepid scripters from around the world, the event continues to grow in size, complexity, and importance in my own mind.


How to only print one line from a script in Python? I want only one , I want only one line, say line 3, to be printed. In this case, the guy wants to print line 3 if some condition is satisfied, and he probably wants to How do I search for a line in a file and print out the location of the found text (which line and col) in Python? Surround the block of stuff you don't want to run with triple quotes. Workbook.PrintOut method (Excel) 05/29/2019; 2 minutes to read; In this article. Prints the object. Syntax. expression.PrintOut (From, To, Copies, Preview


PowerShell: Playing with text or how to get every x line from text , Our next part is to remove the first three lines. unbenannt51.png. This takes us to skip. Let's move on and remove the first 3 lines with Select-  Summary: Microsoft PowerShell MVP, Marco Shaw, discusses writing output from Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Today we will be joined by Marco Shaw. Marco is a consultant with CGI in Moncton. He has been working in the IT industry for over 12 years, also working for Bell Aliant and Atlantic Lottery.


PowerShell One-Liners: Help, Syntax, Display and Files, PowerShell tackles this paradox by providing its own help and You can do quite a lot in one line of PowerShell, such as grabbing the contents of specific elements of Part 2 is rounded out with a few other vital bits on leveraging the PowerShell environment. Non-printable characters (newline, tab, etc.)  Working on a Revit model, I create several 'work sections' to get things done. But when it's time to print, I don't want those sectionlines printed - only the ones that reference a sheet. So I created different section families. Thing is, when I hide the 'work sections' on a sheet, they are also h