[HELP] Label output into multiple lines

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Mitzen
Posts: 15
Last visit: Thu Jan 19, 2017 9:01 am

[HELP] Label output into multiple lines

Post by Mitzen »

Hey all,

When I run "$(Get-CimInstance -ClassName win32_diskdrive | select caption -ExpandProperty caption)" it will list all my connected drives.

When I run this in powershell it breaks the drives line by line as shown below

drive1
drive2
drive3

But when I output my command to a label it puts the drives like so: drive1drive2drive3. How can I properly display the drives? Code listed below

$drives = $(Get-CimInstance -ClassName win32_diskdrive | select caption -ExpandProperty caption)
$label1.Text = $drives
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: [HELP] Label output into multiple lines

Post by jvierra »

Try this:
  1. $captions = Get-CimInstance win32_diskdrive | select -expand caption
  2. $label1.Text = $captions -join "`n"
User avatar
Mitzen
Posts: 15
Last visit: Thu Jan 19, 2017 9:01 am

Re: [HELP] Label output into multiple lines

Post by Mitzen »

That is exactly what I'm looking for.

My next question would be how to get that $caption -join "`n" inside an array?

When I do the following, it gives me a an unexpected token error.

$captions = Get-CimInstance win32_diskdrive | select -expand caption

$info = @( System Info `n

"BIOS Version: $(Get-Ciminstance win32_bios | select -expand SMbiobiosversion) `n"
"Baseboard: $(Get-Ciminstance win32_baseboard | select -expand product) `n"
"Drives: $captions -join "`n""

$label1.Text = $info
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: [HELP] Label output into multiple lines

Post by jvierra »

Like this:
  1. $label1.Text = Get-CimInstance win32_diskdrive |
  2.         select @{ Name = 'Sysetm Info'; e = { $_.caption } } |
  3.         Out-String
I recommend stepping back and taking some time to learn PowerShell before you try using forms. Without good PS knowledge you will be very frustrated by how to do things,
This topic is 7 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked