Write-Information concatenate string and variable

Hello And Welcome :slight_smile:

I’m completely new to PowerShell so please be patient for me :wink:

I try to write to console string “Name:” and parameter returned from Get-CimInstance (this is name of the UPS connected to computer).

I read that Write-Host is outdatet, so I try to use for this Write-Information, but it wont work.

[pre]Write-Information -MessageData “Name:” (Get-CimInstance -ClassName Win32_Battery -Namespace root\CIMV2 | Select-Object -ExpandProperty Name) -InformationAction Continue[/pre]

When I use Write-Host It works flawlewssly.
[pre]Write-Host "Name: " (Get-CimInstance -ClassName Win32_Battery -Namespace root\CIMV2 | Select-Object -ExpandProperty Name)[/pre]

How to concatenate string and returned value in Write-Information?

 

Best Regards

It depends pretty much on what you are trying to accomplish. Write-Host is not outdated - it is actually inappropriate for most of the purposses it is used for. If you really just like to output it to the screen and if you’re not planning to use it further more - use Write-Host.

I prefer write output.

Write-Output -InputObject "Name: $(Get-CimInstance -ClassName Win32_Battery -Namespace root\CIMV2 | Select-Object -ExpandProperty Name)"

Hello Olaf, and thank you for reply :slight_smile:

At this time I only want to display message in console, but in the future I’ll try to log all displayed informations to file.

So thinking about future, Write-Information is better, or should I use Write-Host to display info to console, and then Add-Content to write it to file?

Hello kvprasoon :slight_smile:

I use your code with Write-Information and its working :slight_smile:

[pre]Write-Information -MessageData “Name: $(Get-CimInstance -ClassName Win32_Battery -Namespace root\CIMV2 | Select-Object -ExpandProperty Name)” -InformationAction Continue[/pre]

But to be honest, I don’t understand this syntax…

[pre]“Name: $(Get-CimInstance -ClassName Win32_Battery -Namespace root\CIMV2 | Select-Object -ExpandProperty Name)” [/pre]

Double quotes means that this is string for -MessageData parameter. But what dollar sign means? About what should I read to get more info about that syntax?

If you like to write information to a file you would use Out-File or Set-Content or Add-Content.

But actually you don’t have to re-invent the wheel. There are already modules for logging purposses avaliable. Just search for “logging” in the Powershell Gallery.

@Olaf,

Out-File, Set-Content, and Add-Content looks very promising for my purposes. I must read about differences between them.

Regarding PowerShell Gallery - I don’t want to use content written by someone else, because I want to learn PowerShell. But thank you for sugesstion :slight_smile:

:wink: … you don’t need to use the code from the gallery … you can learn from it.

[quote quote=165919]:wink: … you don’t need to use the code from the gallery … you can learn from it.

[/quote]
Right :slight_smile: Thank you once again :slight_smile:

$( ) is the subexpression operator. You can use it to evaluate expressions inside a double quoted string, among other things. There’s not a lot of documentation for it. It has a mention in about_operators. Notice you can put a string by itself. It’s like write-output “string”. Although write-host has the convenience of making all its arguments one string. I’m all about convenience.

"$(1 + 1)"
2

$a = ps terminal

$a.name
Terminal

"the name is $a.name"
the name is System.Diagnostics.Process (Terminal).name

"the name is $($a.name)"
the name is Terminal

You can also use $( ) outside of strings, anywhere you want to treat multiple statements (including foreach or if) like one statement.

$(echo hi; echo there) | measure

Count             : 2
Average           : 
Sum               : 
Maximum           : 
Minimum           : 
StandardDeviation : 
Property          : 

$( foreach ($i in 1..3) { $i } } | measure  # can't normally pipe from foreach () 
&{ foreach ($i in 1..3) { $i } } | measure  # although I like the call operator better, 
                                            # you don't have to wait

If you wan’t to print and write to a file simultaneously, you can use Tee-Object cmdlet.

Tee-Object -FilePath c:\temp\tee.txt -InputObject "Name: $(Get-CimInstance -ClassName Win32_Battery -Namespace root\CIMV2 | Select-Object -ExpandProperty Name)" -Append

Write-Host is appropriate for scripts which must create host display without affecting script output / function return values.

Emphasis on must because by and large doing so can be obnoxious or unnecessary, and limits the reusability of your code. Ideally, you would have the bulk of your code written entirely without Write-Host, and then you would execute your code with a wrapper script that included the necessary Write-Host lines.

Write-Information is good for similar kinds of information, if you don’t need it to display by default. It will still be there if it is needed, but it does not display by default (although it is logged by default if you have logging enabled).

For most things, I consider Write-Verbose to be more than sufficient; if users want to view it, they can call your script with the -Verbose flag (note that you will need to apply [CmdletBinding()] to your script to enable the verbose messaging properly, however).

In a majority of cases, if you have a “need” for Write-Host it’s because:

  1. You have a lengthy script (in which case you should optimize it and perhaps utilise Write-Progress if the time is an acceptable / unavoidable consequence of what it needs to do)
  2. You're providing user feedback in an interactive script (any interactive portions should be clearly and completely separate from operational parts of the script to maximise reusability)
  3. You're using it because your logging needs something to log (in which case just use Write-Verbose and call the script with -Verbose when you need to log the data -- OR accept that there will be no interactive output and just use Write-Information)

Gentlemen’s I want to thank all of you for such big part of knowledge :slight_smile: Now I have big problem - I must understand all of it :wink:

I will practice suggested solutions and will choose best for my purposes.

Once again - thank you.