2

Consider this simple code:

function Fruits {
    Install-WindowsFeature -Name Telnet-Client

    return @("apple", "banana", "orange")
}

$FruitArray = @(Fruits)

Output of $FruitArray[0]:

Success Restart Needed Exit Code     Feature Result
------- -------------- ---------     --------------
True    Yes            Success       {}

Output of $FruitArray[1], $FruitArray[2], $FruitArray[3]:

apple
banana
orange

What is happening here? How can I clear the unwanted output from the first element of the array, using return?

Using PowerShell version 5.1, Server 2012 R2.

0

2 Answers 2

1

You get the unexpected output because Install-WindowsFeature returns an object.

If you don't care about the result of Install-WindowsFeature you can pipe the result to null like this:

Install-WindowsFeature -Name Telnet-Client *> $null
Sign up to request clarification or add additional context in comments.

1 Comment

For the sake of completion, there's also the Out-Null cmdlet. The above line would then become Install-WindowsFeature -Name Telnet-Client | Out-Null
0

It is formatted properly, have a look here

From the link (emphasis mine):

The Return keyword exits a function, script, or script block. It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached.

Users who are familiar with languages like C or C# might want to use the Return keyword to make the logic of leaving a scope explicit.

In PowerShell, the results of each statement are returned as output, even without a statement that contains the Return keyword. Languages like C or C# return only the value or values that are specified by the Return keyword.

Blockquote

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.