Windows Vista Forums

How can I tell if a function is already defined?

  1. #1


    Kevin Buchan Guest

    How can I tell if a function is already defined?

    Maybe I'm looking at this wrong, but what I want to do is
    conditionally dot source a .ps1 file if it hasn't already been loaded
    earlier in my script.

    I'm making use of the "Should-Process.ps1" script so that I can add
    that great functionality to my own special purpose functions. Each of
    these is currently maintained in separate scripts and each has a
    dependency on the Should-Process.ps1 script. I'd like to just add a
    line to dot source the Should-Process.ps1 file if the Should-Process
    function is not already defined.

    I can't seem to figure out how to tell if it is, though.

    Thanks.

    --
    Kevin Buchan

      My System SpecsSystem Spec

  2. #2


    Brandon Shell [MVP] Guest

    Re: How can I tell if a function is already defined?

    get-content function:<Name>

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    K> Maybe I'm looking at this wrong, but what I want to do is
    K> conditionally dot source a .ps1 file if it hasn't already been loaded
    K> earlier in my script.
    K>
    K> I'm making use of the "Should-Process.ps1" script so that I can add
    K> that great functionality to my own special purpose functions. Each
    K> of these is currently maintained in separate scripts and each has a
    K> dependency on the Should-Process.ps1 script. I'd like to just add a
    K> line to dot source the Should-Process.ps1 file if the Should-Process
    K> function is not already defined.
    K>
    K> I can't seem to figure out how to tell if it is, though.
    K>
    K> Thanks.
    K>



      My System SpecsSystem Spec

  3. #3


    Jon Guest

    Re: How can I tell if a function is already defined?


    if (!(test-path function:\should-process)) {. Should-Process.ps1}

    should do it.

    You'd need to specify the full path to Should-Process.ps1, if it's not in
    your path.

    --
    Jon


    "Kevin Buchan" <kevin.buchan@xxxxxx[Pls Don't Spam]sanders.com> wrote in
    message news:ml7hq39trfk79j49sjq66gdkoeefbmgfsp@xxxxxx

    > Maybe I'm looking at this wrong, but what I want to do is
    > conditionally dot source a .ps1 file if it hasn't already been loaded
    > earlier in my script.
    >
    > I'm making use of the "Should-Process.ps1" script so that I can add
    > that great functionality to my own special purpose functions. Each of
    > these is currently maintained in separate scripts and each has a
    > dependency on the Should-Process.ps1 script. I'd like to just add a
    > line to dot source the Should-Process.ps1 file if the Should-Process
    > function is not already defined.
    >
    > I can't seem to figure out how to tell if it is, though.
    >
    > Thanks.
    >
    > --
    > Kevin Buchan

      My System SpecsSystem Spec

  4. #4


    Kevin Buchan Guest

    Re: How can I tell if a function is already defined?

    That's perfect!

    I knew that there had to be a great solution like this, but never
    considered using 'Test-Path'.

    -Kevin

    On Tue, 5 Feb 2008 17:54:36 -0000, "Jon"
    <Email_Address@xxxxxx> wrote:

    >
    >if (!(test-path function:\should-process)) {. Should-Process.ps1}
    >
    >should do it.
    >
    >You'd need to specify the full path to Should-Process.ps1, if it's not in
    >your path.

      My System SpecsSystem Spec


How can I tell if a function is already defined?
Similar Threads
Thread Forum
Test if a variables is defined or not .NET General
Check if user defined in SD VB Script
nothing highly defined in HD Graphic cards
User-defined sorting Vista file management
BUG: Redirecting function contents to a file truncates function lines at the width of the console PowerShell