Write-Information concatenate string and variable

Welcome Forums General PowerShell Q&A Write-Information concatenate string and variable

This topic contains 12 replies, has 5 voices, and was last updated by

 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #165874

    Participant
    Topics: 3
    Replies: 7
    Points: 56
    Rank: Member

    Hello And Welcome ๐Ÿ™‚

    I'm completely new to PowerShell so please be patient for me ๐Ÿ˜‰

    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.

    Write-Information -MessageData "Name:" (Get-CimInstance -ClassName Win32_Battery -Namespace root\CIMV2 | Select-Object -ExpandProperty Name) -InformationAction Continue

    When I use Write-Host It works flawlewssly.

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

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

     

    Best Regards

  • #165889

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    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.

  • #165901

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    I prefer write output.

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

    Participant
    Topics: 3
    Replies: 7
    Points: 56
    Rank: Member

    Hello Olaf, and thank you for reply ๐Ÿ™‚

    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?

  • #165907

    Participant
    Topics: 3
    Replies: 7
    Points: 56
    Rank: Member

    Hello kvprasoon ๐Ÿ™‚

    I use your code with Write-Information and its working ๐Ÿ™‚

    Write-Information -MessageData "Name: $(Get-CimInstance -ClassName Win32_Battery -Namespace root\CIMV2 | Select-Object -ExpandProperty Name)" -InformationAction Continue

    But to be honest, I don't understand this syntax...

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

    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?

  • #165910

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    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.

  • #165916

    Participant
    Topics: 3
    Replies: 7
    Points: 56
    Rank: Member

    @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 ๐Ÿ™‚

    • #165919

      Participant
      Topics: 1
      Replies: 1530
      Points: 2,591
      Helping Hand
      Rank: Community Hero

      ๐Ÿ˜‰ ... you don't need to use the code from the gallery ... you can learn from it.

  • #165925

    Participant
    Topics: 3
    Replies: 7
    Points: 56
    Rank: Member

    😉 ... you don't need to use the code from the gallery ... you can learn from it.

    Right ๐Ÿ™‚ Thank you once again ๐Ÿ™‚

  • #165928
    js

    Participant
    Topics: 25
    Replies: 678
    Points: 1,629
    Helping Hand
    Rank: Community Hero

    $( ) 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
    
  • #165955

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    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
    
  • #166000

    Participant
    Topics: 2
    Replies: 483
    Points: 1,152
    Helping Hand
    Rank: Community Hero

    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)
  • #166103

    Participant
    Topics: 3
    Replies: 7
    Points: 56
    Rank: Member

    Gentlemen's I want to thank all of you for such big part of knowledge ๐Ÿ™‚ Now I have big problem – I must understand all of it ๐Ÿ˜‰

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

    Once again – thank you.

You must be logged in to reply to this topic.