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
>

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.

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

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:

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.

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.