Home > Uncategorized > Powershell: Multiple returns

Powershell: Multiple returns

I stumbled upon a unexpected behavior in Powershell the other day. A colleague of mine hade a code snippet similar to the following lines:

function create-directory($name)
{
    md $name
    return (test-path $name)
}

$result = create-directory "c:\temp\temp2"
$result

I expected $result to contain true but… no! Instead it was an array with two elements; one of type DirectoryInfo and one boolean. It turns out that Powershell allows multiple returns. This means that we can write functions like:

function multiple-returns
{
    "John"
    "Robert"
    "Richard"
}

Such a function will return an array of three string elements; John, Robert and Richard(!).

Advertisements
Categories: Uncategorized
  1. May 9, 2014 at 8:02 am

    Hi there, yeɑh this post is in fact pleasant and I have learned lot
    of thiոgs from it regarding blogging. thanks.

  2. May 9, 2014 at 7:52 pm

    Glad you like it. Thanks!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: