Mikey
Mikey

Reputation: 133

Setting line limit in Multiline Textbox in powershell

I have been struggling to figure out how to use the "Out-File" from a textbox to a .txt and limit the amount of lines to that file. I have been able to do this the other way around with Get-content and using -totalcount 5 when sorting from a TextFile to Textbox.

I have this right now. I just do not know how to limit the amount of lines that get pulled from box $objOutputBox.Text to the file chines.txt. If I have say 20 lines that are names descending in $objOutputBox.Text and I only want 10 to pull over, this is what I'm trying to accomplish.

$objOutputBox.Text | Out-File C:\temp\PROD\chines.txt 

Is there a easy way to set a line limit when using the Out-File in Poweshell? The other issue I was running into is that when I type line by line into the textbox and click_button to Out-File the lines they are placed descending to chines.txt. When I use Get-Content to grab some lines from another file (that is descending in the .txt) to fill the textbox(descending), then I use the Out-File to push those same lines to chines.txt they are placed as such below.

kvmlcops0263cdc kvmlcops0264bdc kvmlcops0264cdc kvmlcops0265bdc kvmlcops0265cdc

instead of

kvmlcops0263cdc

kvmlcops0264bdc

kvmlcops0264cdc

kvmlcops0265bdc

kvmlcops0265cdc

When I type the names into the box directly and use the Out-File they are placed into the txt as descending above. Is there a reason for this even though in the textbox they are descending?

Your help is highly desired and much appreciated.

Thanks,

Michael

Upvotes: 0

Views: 5322

Answers (2)

Jackie
Jackie

Reputation: 2634

For the first question, Andy give a good example, for the second question, I think it's because you assign the output of get-content directly to $text_box.Text like

$lines = Get-Content "temp.txt"
$text_box.Text = $lines

So that the string array $lines is joined into a string automatically by PowerShell, you need

$text_box.Text = $lines -join "`n"

Upvotes: 1

Andy Arismendi
Andy Arismendi

Reputation: 52619

Try this:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")    
$text_box = New-Object System.Windows.Forms.TextBox     
$text_box.Multiline = $true 
$text_box.Text = "cat`nhat" 
$text_box.Text.Split("`n") | Select -First 1 | Out-File output.txt

Split the text on the newline character `n and use Select-Object to filter the quantity you want.

Upvotes: 1

Related Questions