Windows Vista Forums

When to use write-host?
  1. #1


    how to display variable value? Guest

    When to use write-host?

    Suppose I have a variable initialized like

    $abc = kapil

    then what is the difference between

    write-host $abc



    and

    directly writing $abc on console

      My System SpecsSystem Spec

  2. #2


    /\\/\\o\\/\\/ Guest

    Re: When to use write-host?

    directly write to the console is the same as out-host.

    write-host does a tostring() on the objects for displaying forcing the
    object to string format, but can be used for collored output.
    but we aware that write-host sometimes can give a other output as expected :

    some samples that might help.

    PS C:\Users\mvanorsouw> $a = 1,2,3
    PS C:\Users\mvanorsouw> write-host $a
    1 2 3
    PS C:\Users\mvanorsouw> out-host $a
    Out-Host : A positional parameter cannot be found that accepts argument
    'System.Object[]'.
    At line:1 char:9
    + out-host <<<< $a
    PS C:\Users\mvanorsouw> out-host -InputObject $a
    1
    2
    3
    PS C:\Users\mvanorsouw> $a | write-host -ForegroundColor yellow
    1
    2
    3

    (I wonder whether I can cal the error a bug)


    Greetings /\/\o\/\/
    http://thePowerShellGuy.com

    "how to display variable value?" <how to display variable
    value?@discussions.microsoft.com> wrote in message
    news:6AF2A622-5CCA-4C9D-AC51-D1ECA99E4131@xxxxxx

    > Suppose I have a variable initialized like
    >
    > $abc = kapil
    >
    > then what is the difference between
    >
    > write-host $abc
    >
    > and
    >
    > directly writing $abc on console

      My System SpecsSystem Spec

When to use write-host? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Display differences Write-Host and Write-Warning TreyS PowerShell 1 01 Jan 2010
write-host? no thanks Joe PowerShell 2 10 Dec 2008
1..9|%{write-host $_:443} PowerShell 11 21 Jul 2007
redirect write-host DouglasWoods PowerShell 12 26 Apr 2007
Write-Host question Doug PowerShell 2 20 Dec 2006