Windows Vista Forums

Test for an object that is nothing

  1. #1


    Dave Coate Guest

    Test for an object that is nothing

    Hi

    Is there a way in powershell to determine if an object is nothing?

    In vb.net I would use:

    If var is nothing then ...

    What is the Powershell equivalent?

    Dave Coate, MCSE



      My System SpecsSystem Spec

  2. #2


    Brandon Shell Guest

    Re: Test for an object that is nothing

    if(!($object)){"nothing"}

    "Dave Coate" <David.CoateNoSpam@alteontrainingNoSpam.com> wrote in message
    news:%23495U0srHHA.4180@TK2MSFTNGP04.phx.gbl...
    > Hi
    >
    > Is there a way in powershell to determine if an object is nothing?
    >
    > In vb.net I would use:
    >
    > If var is nothing then ...
    >
    > What is the Powershell equivalent?
    >
    > Dave Coate, MCSE
    >



      My System SpecsSystem Spec

  3. #3


    Jacques Barathon [MS] Guest

    Re: Test for an object that is nothing

    "Dave Coate" <David.CoateNoSpam@alteontrainingNoSpam.com> wrote in message
    news:%23495U0srHHA.4180@TK2MSFTNGP04.phx.gbl...
    > Hi
    >
    > Is there a way in powershell to determine if an object is nothing?
    >
    > In vb.net I would use:
    >
    > If var is nothing then ...
    >
    > What is the Powershell equivalent?


    Do you want to test whether var contains something or not? In PowerShell,
    you can use $null:

    if ($var -eq $null) {...}

    If you want to test whether $var exists or not, you can dir the variable:
    drive (yeah, sounds weird :-)):

    if (! (dir variable:var 2> $null)) {...}

    I redirect the error stream to $null to avoid having the output filled in
    with the error message indicating that var does not exist.

    There might be other, more conventional ways... Hope that helps.

    Jacques


      My System SpecsSystem Spec

  4. #4


    Marcel J. Ortiz [MSFT] Guest

    Re: Test for an object that is nothing

    > if (! (dir variable:var 2> $null)) {...}
    >
    > I redirect the error stream to $null to avoid having the output filled in
    > with the error message indicating that var does not exist.


    A better way to test for existence of a path is to use the test-path cmdlet.



    "Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
    news:e%23s5SBtrHHA.2368@TK2MSFTNGP05.phx.gbl...
    > "Dave Coate" <David.CoateNoSpam@alteontrainingNoSpam.com> wrote in message
    > news:%23495U0srHHA.4180@TK2MSFTNGP04.phx.gbl...
    >> Hi
    >>
    >> Is there a way in powershell to determine if an object is nothing?
    >>
    >> In vb.net I would use:
    >>
    >> If var is nothing then ...
    >>
    >> What is the Powershell equivalent?

    >
    > Do you want to test whether var contains something or not? In PowerShell,
    > you can use $null:
    >
    > if ($var -eq $null) {...}
    >
    > If you want to test whether $var exists or not, you can dir the variable:
    > drive (yeah, sounds weird :-)):
    >
    > if (! (dir variable:var 2> $null)) {...}
    >
    > I redirect the error stream to $null to avoid having the output filled in
    > with the error message indicating that var does not exist.
    >
    > There might be other, more conventional ways... Hope that helps.
    >
    > Jacques



      My System SpecsSystem Spec

  5. #5


    Keith Hill [MVP] Guest

    Re: Test for an object that is nothing

    "Brandon Shell" <tshell.mask@gmail.com> wrote in message
    news:uh3iW6srHHA.1864@TK2MSFTNGP04.phx.gbl...
    > if(!($object)){"nothing"}
    >


    Careful, I've burned myself on code like that. It is usually better to test
    against $null e.g.:

    PS> $object = 0
    PS> if (!$object) {"nothing"}
    nothing

    I can plenty of cases where the value 0 is a valid value and not to be
    confused with "nothing".

    --
    Keith


      My System SpecsSystem Spec

  6. #6


    Keith Hill [MVP] Guest

    Re: Test for an object that is nothing

    "Marcel J. Ortiz [MSFT]" <mosoto@online.microsoft.com> wrote in message
    news:eZtL5EtrHHA.5032@TK2MSFTNGP04.phx.gbl...
    >> if (! (dir variable:var 2> $null)) {...}
    >>
    >> I redirect the error stream to $null to avoid having the output filled in
    >> with the error message indicating that var does not exist.

    >
    > A better way to test for existence of a path is to use the test-path
    > cmdlet.
    >


    Yep we do this enough in PSCX that we define a special function during
    profile processing to make testing for the existence and value of a
    variable, just a simple function call:

    function Test-PscxPreference($name)
    {
    if (Test-Path "Variable:$name")
    {
    (Get-Variable $name).Value
    }
    else
    {
    $false
    }
    }

    --
    Keith


      My System SpecsSystem Spec

  7. #7


    William Stacey [C# MVP] Guest

    Re: Test for an object that is nothing

    2 cent nit. I would remove "nothing" from the vocab. Something is null or
    it is not null. A reference type var can be null or not null. A value type
    var (i.e. int, long, etc) can not be null (there are nullable types in 2.0
    which are special value types). psh does a pretty good job of hidding (i.e.
    wrapping) these differences so a var can magically be "null" or contain
    something like an interger.

    --
    William Stacey [C# MVP]



    "Keith Hill [MVP]" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
    news:C31FB85D-A08F-4596-8C2B-9EA4C8651116@microsoft.com...
    | "Brandon Shell" <tshell.mask@gmail.com> wrote in message
    | news:uh3iW6srHHA.1864@TK2MSFTNGP04.phx.gbl...
    | > if(!($object)){"nothing"}
    | >
    |
    | Careful, I've burned myself on code like that. It is usually better to
    test
    | against $null e.g.:
    |
    | PS> $object = 0
    | PS> if (!$object) {"nothing"}
    | nothing
    |
    | I can plenty of cases where the value 0 is a valid value and not to be
    | confused with "nothing".
    |
    | --
    | Keith
    |



      My System SpecsSystem Spec

  8. #8


    Jacques Barathon [MS] Guest

    Re: Test for an object that is nothing

    "Marcel J. Ortiz [MSFT]" <mosoto@online.microsoft.com> wrote in message
    news:eZtL5EtrHHA.5032@TK2MSFTNGP04.phx.gbl...
    >> if (! (dir variable:var 2> $null)) {...}
    >>
    >> I redirect the error stream to $null to avoid having the output filled in
    >> with the error message indicating that var does not exist.

    >
    > A better way to test for existence of a path is to use the test-path
    > cmdlet.


    Right! I just hadn't gone that far yet in the drive/path analogy :-)

    Jacques


      My System SpecsSystem Spec


Test for an object that is nothing
Similar Threads
Thread Forum
Casting and iteration in object of type object .NET General
Binding TextBox.Text to property in nested (multi levels) object withbindingsource, Error if grandchild object is NULL .NET General
Inherit from usercontrol - Object not set to instance of an object .NET General
Testing object arrays using Compare-Object and -contains PowerShell
Adding canonical aliases for Compare-Object, Measure-Object, New-Object PowerShell