1.    14 Jun 2007 #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.    14 Jun 2007 #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.    14 Jun 2007 #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.    14 Jun 2007 #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.    14 Jun 2007 #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.    14 Jun 2007 #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.    14 Jun 2007 #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.    15 Jun 2007 #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


Similar Threads
Thread Forum
Get Currently Existent Object Not Created with New-Object - PowerShell
Basically I have a script where it logs into a site and browses to a particular page. It then clicks a link that opens a popup window. I'm trying...
General Discussion
Casting and iteration in object of type object
Hi I have a function in a dll that returns an object which I need to get somes items as follows public object CreateFacts(RuleSetInfo...
.NET General
Binding TextBox.Text to property in nested (multi levels) object withbindingsource, Error if grandchild object is NULL
Hi all, I am binding a TextBox to an object which has child object which has a grandchild object, and I want to bind to the property of the...
.NET General
Inherit from usercontrol - Object not set to instance of an object
I am trying to inherit from a user control, but when i try to change the value of a property of a control within the base control, i get the above...
.NET General
datalist -- Object reference not set to an instance of an object.
I have a page with a datalist that on load works fine but on an edit postback I get the error "Object reference not set to an instance of an...
.NET General
Testing object arrays using Compare-Object and -contains
I'm having trouble finding a simple scheme for getting all objects which are members of a set B but not also members of another set A. The...
PowerShell
Adding canonical aliases for Compare-Object, Measure-Object, New-Object
Vote on this, please. :) https://connect.microsoft.com/feedback/ViewFeedback.aspx?SiteID=99&FeedbackID=79879 Of the 9 object cmdlets, 6 have...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • PC Help Forum
  • 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 16:59.
    .