Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output text in multiline

I am fighting with outputting text file and getting file content in exactly the same way as it was provided by the user. I tried to find an answer but the only thing I can find is how to change multiline into one string which is quite the opposite I need.

How to ensure that input provided by the user in multiline will not be altered to single line in PowerShell?


Details:

In inputbox I provide details in multiline:

enter image description here

PowerShell saves the output in one line:

enter image description here

I want PowerShell to save multiline input and then show multiline output.

Here is the code:

function Notes
{

$var = $Users.Text 
$var | Out-File C:\Users\A570654\Desktop\users.txt
$var = Get-Content C:\Users\A570654\Desktop\users.txt


Set-Content -Path C:\Users\A570654\Desktop\notes.txt "*************************************************
$var
*************************************************"


Invoke-Item -Path C:\Users\A570654\Desktop\notes.txt
}

############################## FORMS #######################################

$Users = New-Object System.Windows.Forms.RichTextBox
$Users.Size = New-Object System.Drawing.size(470,85)
$Users.Location = New-Object System.Drawing.Size(10,100)
$Users.MultiLine = $true
$Users.AutoSize = $true
$Users.ScrollBars = "Vertical" 
$users.AcceptsTab

$form = New-Object system.windows.forms.form
$form.size = New-Object system.drawing.size(600,600)

$button = New-Object system.windows.forms.button
$button.text = "Get Notes"
$button.Add_Click({Notes})


$form.Controls.Add($Users)
$form.Controls.Add($button)
$form.Showdialog()

The reason why I decided to save the string in file is that output shows spaces as in picture 2. If I decide not to save output to file and then get its content then string appears in the following format: JohnTomKate.

like image 285
Maciej.G Avatar asked Oct 23 '25 05:10

Maciej.G


1 Answers

You are saving the user input to a file and reload the content which will give you the current output. So just use the variable without reassigning it:

$var = $Users.Text 
$var | Out-File C:\Users\A570654\Desktop\users.txt

Set-Content -Path C:\Users\A570654\Desktop\notes.txt "*************************************************
$var
*************************************************"

Output:

*************************************************
John 
Tom 
Kate
*************************************************
like image 183
Martin Brandl Avatar answered Oct 25 '25 18:10

Martin Brandl