Nico Nekoru
Nico Nekoru

Reputation: 3112

Is there a way to echo multiple objects at once?

I want to find out if I could echo multiple objects in powershell. I tried searching this and found this, but It wasn't what I was looking for.

Basically, I want to be able to echo two different objects (that can both be seen in foreach-object) with a single command.

Expected input & output

PS C:\> <echo multiple object command> "test" | % {echo "$_ test"}
test test
test test

Input and output for what I've tried.

PS C:\> echo "test`ntest" | % {echo "$_ test"}
test
test test 

Expected output:

test test
test test

If I do this with get-process for example, like so:

PS C:\> get-process | % {echo "$_ and this"}

Output:

Process1 (Thing) and this
Process2 (Thing) and this
Process3 (Thing) and this
Process4 (Thing) and this

This will also work with arrays like so:

PS C:\> $test = @("test1","test2","test3")
PS C:\> $test | % {echo "$_ and this"}
test1 and this
test2 and this
test3 and this

But so far I have not found any way to replicate this with specifying the certain objects other than arrays

Summary: Is there a way to write a multiple object output in powershell 5.1?

So all in all, I want to echo a multi-object output?

Upvotes: 1

Views: 898

Answers (3)

marsze
marsze

Reputation: 17064

In short: You can only send one thing to the pipeline at a time. Be it a string, an array of strings, or any other object.

What you are asking is not to output multiple objects, but basically just about the way the output is displayed and formatted.

PowerShell is object-based and not text-based. Displayed output is not content. It has default formats to display objects in a convenient and readable way, even some special formats to display certain object types in a familiar fashion (e.g. file system items).

If I understand correctly, you are trying to display multiple objects "next to each other". This is a very text-oriented approach (not object-oriented). I don't recommend it.

Basically, it's just a formatting question. Ask yourself: How do you expect the output to look like? Then, find a way to do it. Some simple examples were already mentioned in the other answers. You may also want to write a custom formatting file.

In the end, it all depends on your personal preferences. Ask yourself, do you really need it? And why? This might be an XY problem.

Upvotes: 0

Jawad
Jawad

Reputation: 11364

You can combine the output into a single line by using the -join method.

($test | % {echo "$_ and this"}) -join ","
# test1 and this,test2 and this,test3 and this

If you are interested in printing an array with a single output, you can quote the array to print it with a single command

Write-Output "$test"
# test1 test2 test3

what I dont get is how you expect the following to print anything but.

echo "test`ntest" | % {echo "$_ test"}
test
test test

Basically you are piping the string (test'ntest) to a foreach object which iterates only one time because there is only one string (test'ntest) and then print that (contained within $_) with another " test".

Here is another example of how you can print get-service with one command (without looping)

Write-output (get-service)

Upvotes: 0

Gerrit Geeraerts
Gerrit Geeraerts

Reputation: 1174

According to microsoft docs

The ForEach-Object (%) cmdlet performs an operation on each item in a collection of input objects.

And a collection is other words an array So you realy need to feed an array to the foreach (%) cmdlet

Some other examples:

@("test1","test2","test3").gettype() # system.array

(get-process).gettype() # system.array

(1..10).gettype() # system.array
1..10 | % {echo "$_ test"}

"test`ntest".Split("`n").gettype() #system array
"test`ntest".Split("`n") | % {echo "$_ test"}

in your first example: echo "test`ntest" | % {echo "$_ test"}

when we check what (echo "testntest").gettype()` is we can see its a System.Object and not an array. so the foreach object will see it as an array with 1 element inside.

Upvotes: 0

Related Questions