1

I'm running a PowerShell script, which, call another script by itself. In the first script, when I put echo, everything is OK, but I can't get any echo in the second ("client") script to show anything.

How can I get echo work?

I'm using PowerShell 2.0 on Windows 7 Pro, with PowerGUI. (I also tried with ISE, but it does not help)

3 Answers 3

2

You should use Write-Host directly insted of echo (witch is alias for Write-Output).

Sign up to request clarification or add additional context in comments.

5 Comments

It seems to be working now, but another problem: I want to print full properties of the object, but Write-Host only print their type. How can I fix that?
you should never use Write-Host to return or output something in a script. It is a best practice to always use Write-Output
ravikanth If the SO wants to write to the console, but not the pipeline then using write-host is the correct thing to do. The question doesn't make this distinction clear.
artem Write-Host is not an alias for Write-Output AFAIK it works differently in that it outputs to the console only, rather than to the script "output" pipeline.
@chibacity, see the first comment. He wants to print all properties. That can be done either using Format-List * or using Write-Output. I agree on the pipeline part of your response.
1

A sample of your script would help in giving an accurate answer. That said, I tried Write-Output from nested scripts and everything seems to working for me.

#Script1.ps1
Write-Output "Script 1"
Write-Output "Calling Script 2"
./Script2.ps1

#Script2.ps1
Write-Output "Script 2"
Write-Output "Script 2 End"

And, the output:

PS> .\Script-one.ps1
Script 1
Calling Script 2
Script 2
Script 2 End

1 Comment

Seems like @Vimvq1987 calls that script indirectly. i.e. using powershell.exe
1

You can use Write-Host to write to the console ( host)

There seems to be some confusion on using Write-Output vs Write-Host (echo is alias for Write-Output - you can see it from Get-Alias ). Write-Output is not just writing to console. From the docs:

Sends the specified objects to the next command in the pipeline. If the command is the last command in the pipeline, the objects are displayed in the console.

1 Comment

That's not what Artem was saying. He was saying that echo is an alias of write-output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.