September 15th, 2007

Outputting vs. Emitting Objects

PowerShell Team
PowerShell Team

Sunil wrote:

Hello,

Firstly i am sorry to post an unrelated question to this post. However i dont know where to post my questions. Here is my question
Say I have 2 cmdlet’s
Get-location
Get-service
When I create a test.ps1 file out of these 2 command and then run it, it will always do a
Get-service |fl  in the output
Ie. A full listing of the second command. Is there a way we can avoid this?

thanks,

As a general rule, the best place to ask questions is the PowerShell Newsgroup: Microsoft.Public.Windows.PowerShell

What you are seeing with your script is that your script does not PRINT (“OUTPUT to the HOST”) the LOCATION and then the SERVICEs. Instead it EMITS them (“WRITES to the OUTPUT STREAM”).

Unless you connect things otherwise, the objects on the output stream are sent to some code that will output them to the host AS A COLLECTION. The way that works is that the first object in the collection is used to determine how the rest of the collection will be output. If that code finds something unanticipated in the collection, it just formats it as a list and continues. You can prove this to yourself by doing the following:

PS> gps idle;pwd;gps system
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
——- —— —– —– —– —— — ———–
0 0 0 28 0 0 Idle

Drive : C
Provider : Microsoft.PowerShell.Core\FileSystem
ProviderPath : C:\ps
Path : C:\ps

664 0 0 16872 20 4 System

What you want to do is:

Get-Location | Out-Host

Get-Service | Out-Host

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Category
PowerShell

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.