[POWERSHELL] Read-Host priority

Hello,

I have one question because i don’t understand why.

I have realized an array like this :

$table =@([pscustomobject]@{Commentaire = ‘’})
$path = c:\test\test.txt
$Data = Get-Content -Path $path
$comment = $Data[0].Split("`r")

$table[0].Commentaire = $comment
$table

and the display is like this :

Commentaire

<hr />

contents of the .txt

second content of .txt

 

But when after the code above I add this ligne : Read-Host “Something”

when i start the script my table don’t appear and when I remove Read-host

that works perfectly I don’t know why have you an idea ? thanks you

Are you trying to display a comment before the Read-Host?

$table =[pscustomobject]@{Commentaire = 'This is my content'}

$table.Commentaire
Read-Host "What is up?"

Output:

This is my content
What is up?: The sky
The sky

Or you can additionally concatenate the string with line breaks:

Read-Host ("{0}rnrnWhat is up?" -f $table.Commentaire)

This is my content

What is up?: The moon
The moon

[quote quote=231085]Are you trying to display a comment before the Read-Host?

PowerShell
5 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
$table =[pscustomobject]@{Commentaire = 'This is my content'}
$table.Commentaire
Read-Host "What is up?"
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Output:
PowerShell
4 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
This is my content
What is up?: The sky
The sky
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Or you can additionally concatenate the string with line breaks:
PowerShell
7 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
Read-Host ("{0}rnrnWhat is up?" -f $table.Commentaire)
This is my content
What is up?: The moon
The moon
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] I'm trying to display the comment in a file .txt in my array like this picture ( random pic on google) :

But my comment is put instead of User1, User2…

 

I tried to put my code before the read-Host but it pass in first i don’t know why.

The last solution that you propose don’t correspond for what I want because i want to display $Table before read-Host so i can’t concatenateI think

I think perhaps it is something similar to write-host. Write-host goes directly to the console, which can make it show up before other write-output commands that were executed long before write-host. Take this example

write-output (get-date | select * | gm)
write-host "test"

When the command write-output is called, it takes a split second to calculate the output. Therefore the actual output shows up reverse of the order one might expect.

test

   TypeName: Selected.System.DateTime

Name        MemberType   Definition                                             
----        ----------   ----------                                             
Equals      Method       bool Equals(System.Object obj)                         
GetHashCode Method       int GetHashCode()                                      
GetType     Method       type GetType()                                         
ToString    Method       string ToString()                                      
Date        NoteProperty datetime Date=2020-05-26 12:00:00 AM                   
DateTime    NoteProperty System.String DateTime=Tuesday, May 26, 2020 2:27:37 PM
Day         NoteProperty int Day=26                                             
DayOfWeek   NoteProperty DayOfWeek DayOfWeek=Tuesday                            
DayOfYear   NoteProperty int DayOfYear=147                                      
DisplayHint NoteProperty DisplayHintType DisplayHint=DateTime                   
Hour        NoteProperty int Hour=14                                            
Kind        NoteProperty DateTimeKind Kind=Local                                
Millisecond NoteProperty int Millisecond=886                                    
Minute      NoteProperty int Minute=27                                          
Month       NoteProperty int Month=5                                            
Second      NoteProperty int Second=37                                          
Ticks       NoteProperty long Ticks=637261000578866934                          
TimeOfDay   NoteProperty timespan TimeOfDay=14:27:37.8866934                    
Year        NoteProperty int Year=2020 

As opposed to this

write-output (get-date)
write-host "test"

Tuesday, May 26, 2020 2:32:24 PM
test

If this is the case, perhaps you can assign it to a variable and then output it when you’re ready. (just remember write-host nuance)

$userinput = Read-Host -Prompt "Enter some input"
write-host $userinput

[quote quote=231169]I think perhaps it is something similar to write-host. Write-host goes directly to the console, which can make it show up before other write-output commands that were executed long before write-host. Take this example

PowerShell
3 lines
 
1
2
3
write-output (get-date | select * | gm)
write-host "test"
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
When the command write-output is called, it takes a split second to calculate the output. Therefore the actual output shows up reverse of the order one might expect.
PowerShell
25 lines
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
test
TypeName: Selected.System.DateTime
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Date NoteProperty datetime Date=2020-05-26 12:00:00 AM
DateTime NoteProperty System.String DateTime=Tuesday, May 26, 2020 2:27:37 PM
Day NoteProperty int Day=26
DayOfWeek NoteProperty DayOfWeek DayOfWeek=Tuesday
DayOfYear NoteProperty int DayOfYear=147
DisplayHint NoteProperty DisplayHintType DisplayHint=DateTime
Hour NoteProperty int Hour=14
Kind NoteProperty DateTimeKind Kind=Local
Millisecond NoteProperty int Millisecond=886
Minute NoteProperty int Minute=27
Month NoteProperty int Month=5
Second NoteProperty int Second=37
Ticks NoteProperty long Ticks=637261000578866934
TimeOfDay NoteProperty timespan TimeOfDay=14:27:37.8866934
Year NoteProperty int Year=2020
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
As opposed to this
PowerShell
6 lines
 
1
2
3
4
5
6
write-output (get-date)
write-host "test"
Tuesday, May 26, 2020 2:32:24 PM
test
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
If this is the case, perhaps you can assign it to a variable and then output it when you’re ready. (just remember write-host nuance)
PowerShell
3 lines
 
1
2
3
$userinput = Read-Host -Prompt "Enter some input"
write-host $userinput
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

Yes that is exactly the problem the Read-Host is realized before my Table but when I enter any character when the Read-Host ask i Have in first what I have typing when the Read-Host asking me and in the second my table that is exactly the exemple that you give with Write-Host.

But I tried to do :

$choice = Read-Host -Prompt “test verify display :”

Write-Host $choice

But that doesn’t fix the problem like you said there is always the nuance

 

Do write-information or write-verbose instead of write-host

[quote quote=231205]Do write-information or write-verbose instead of write-host

[/quote]
I tried the two of them but no one work there is no way to resolve it or we can’t use a similar fonction of Read-Host ?

Try write-output then. It may clutter your output.

I just tried and that doesn’t work too the display is always after the read-Host

 

EDIT : I found a way to bypass the problem I have added another Read-Host before the first that doesn’t resolve the problem but that allow to bypass it.