Silly newbie question

  • Hello,

    I am just starting to work with PowerShell, and have the most basic problem that I am sure is due to my misunderstanding something very fundamental. Let's say I create a simple, 1-line script that consists of: Write-Host "Hello". I can invoke the script from the PowerShell command line and it displays "Hello" just as you would expect. I next tried to place the same one line within a function, as:

    Function Test-Hello

    {

    Write-Host "Hello"

    }

    Nothing I do can get this command to display anything. I don't get any error, but it simply returns to the command line with no output displayed. I must be missing something really simple, but I can't figure out what it is. Please help!

    Thanks!

  • If the only contents of your script are the function definition you showed, then all your script does is define a function...now you have to call it 🙂

    Try running a script with these contents:

    Function Test-Hello

    {

    Write-Host "Hello"

    }

    Test-Hello

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Thanks very much for the response! Yep, doing that worked fine. So now my question is, if I have a function (and only the function) defined in a file, is there any way to invoke it from the PowerShell command line? Does a function only execute if you do something like, say, pipe the output of something else into it to work on? I guess I am not understanding the concept of how a user-created function fits into the general scheme of things, and how a function can be invoked.

    Sorry for the basic questions!

  • rickeb1 (4/20/2012)


    Thanks very much for the response! Yep, doing that worked fine. So now my question is, if I have a function (and only the function) defined in a file, is there any way to invoke it from the PowerShell command line?

    Not that I know of. It sounds like you really want to be setting up scripts that accept parameters and then calling those scripts from a PS prompt. Something like this saved as a script named add.ps1:

    param([int]$x, [int]$y)

    $z=$x+$y

    $z

    would be callable from a PS prompt like this:

    Windows PowerShell

    Copyright (C) 2009 Microsoft Corporation. All rights reserved.

    PS C:\> C:\add.ps1 1 2

    3

    PS C:\>

    rickeb1 (4/20/2012)


    Does a function only execute if you do something like, say, pipe the output of something else into it to work on? I guess I am not understanding the concept of how a user-created function fits into the general scheme of things, and how a function can be invoked.

    Sorry for the basic questions!

    They are very good questions. The "pipe the output of something else into it" is the most interesting concpet in PowerShell in my opinion. You are alluding to the PowerShell "Pipeline", i.e. the ability to string multiple commands together where each command passes its result to the next command, sort of like a program all written in one line of code. If you find that using a script that accepts parameters is not good enough and you want to use your code within the Pipeline then I think you would need to forego functions and scripts that accept parameters and go towards creating your own CmdLet...which is only something I only understand in theory...I have yet to write my own CmdLet because I have not found a need for one yet. A CmdLet is what all the built-in PowerShell commands really are, e.g. Write-Host is a CmdLet and I could rewrite your code above to use the Pipeline like this:

    "Hello" | Write-Host

    where "Hello" is actually "piped" into the Write-Host CmdLet instead of explicitly passed in as a parameter as you had it.

    Scripts that accept parameters with functions defined within them offer a lot of power and flexibility and have allowed me to accomplish all tasks I have tried to tackle thus far...but you make me want to knock the "write my first CmdLet" item off my to-do list 😉

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Many, many thanks for the detailed explanations! I think I am getting on the right path now thanks to this discussion. I really appreciate the help!

  • Anytime. Thanks for the positive feedback!

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply