Powershell and writing functions

Discussion in 'Scripting' started by Sanjeev, May 31, 2006.

  1. Sanjeev

    Sanjeev Guest

    I've googled and have not been able to find how to properly write functions
    that return multiple variables. I've seen examples of functions that return
    one variable but not mutliple variables.

    In VBScript, you could use 'byref' to return variables but how do you do
    that in powershell.

    Any examples of functions and the calling method would be greatyly
    appreciated. Thanks
     
    Sanjeev, May 31, 2006
    #1
    1. Advertisements

  2. you can
    MowPS>function fooBar {$foo = 'foo';$bar = 'bar';return $foo,$bar}
    MowPS>foobar
    foo
    bar
    MowPS>(foobar)[0]
    foo
    MowPS>(foobar)[1]
    bar

    you do not need return :

    function fooBar {$foo = 'foo';$bar = 'bar';$foo,$bar}

    you can also use byref in powershell :

    MowPS>$foo = "bar"
    MowPS>$foo
    bar
    MowPS>function barFoo ([ref]$foo){$foo.value = 'foo'}
    MowPS>barfoo $foo
    barFoo : Reference type is expected in argument.
    At line:1 char:7
    + barfoo <<<< $foo
    MowPS>barfoo ([ref]$foo)
    MowPS>$foo
    foo

    hth,

    gr /\/\o\/\/

    P.S. there is also a microsoft.public.windows.Powershell newsgroup now
     
    /\\/\\o\\/\\/, May 31, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.