How can I tell if a function is already defined?


  1.    05 Feb 2008 #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.    05 Feb 2008 #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.    05 Feb 2008 #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.    06 Feb 2008 #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
Re: User defined functions.
Il giorno Fri, 17 Jul 2009 23:22:52 -0600, "Al Dunbar" <alandrub@xxxxxx> ha scritto: Thanks for your reply. Giovanni -- Giovanni Cenati...
VB Script
Test if a variables is defined or not
I've got an unusual programming situation - in VBScript ASP.NET, can some please tell me, how can I test if a variable has been defined with a Dim...
.NET General
Check if user defined in SD
Hi, How would you check if a specific user given to you exists anywhere in the domain please? E.G user id is xxx123456 You want to see if that...
VB Script
nothing highly defined in HD
well i bought viewsonic 22" HD lcd monitor and installed the nvidia drivers and all and also connected it through my DVI cable to my gefrce 8600GT...
Graphic cards
User-defined sorting
Is it possible to create a user-defined field that will allow me to custom-sort my picture directories? I've got lots of photos and videos of my...
Vista file management
2 functions defined, one is ok, the other cannot be found
My Microsoft.PowerShell_profile.ps1 contains two functions. One works fine but the other cannot be found. I am stumped. ## write-host "My bag...
PowerShell
BUG: Redirecting function contents to a file truncates function lines at the width of the console
In my profile.ps1 file, I have various functions with line widths greater than 80 columns. My console is 80 columns. If I use the following: ...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 01:53.
    .