Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to check if an PowerShell Object exist?

I am looking for the best way to check if a Com Object exists.

Here is the code that I have; I'd like to improve the last line:

$ie = New-Object -ComObject InternetExplorer.Application $ie.Navigate("http://www.stackoverflow.com") $ie.Visible = $true  $ie -ne $null #Are there better options? 
like image 203
LaPhi Avatar asked Dec 13 '10 14:12

LaPhi


2 Answers

I would stick with the $null check since any value other than '' (empty string), 0, $false and $null will pass the check: if ($ie) {...}.

like image 78
Keith Hill Avatar answered Sep 23 '22 14:09

Keith Hill


You can also do

if ($ie) {     # Do Something if $ie is not null } 
like image 23
ravikanth Avatar answered Sep 20 '22 14:09

ravikanth