Best way to check if a PowerShell object exists?

I am looking for a better way to check if a Com object exists.

Here is the code I have; I would 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? 
+72
object null powershell
Dec 13 2018-10-13
source share
7 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) {...} .

+94
Dec 13 '10 at 16:39
source share

You can also do

 if ($ie) { # Do Something if $ie is not null } 
+51
Dec 13 '10 at 15:22
source share

In your specific example, you may not need to perform any checks at all. Is it possible that New-Object returns null? I have never seen this. The command does not work in the event of a failure, and the rest of the code in the example will not be executed. So why do these checks at all?

Only in the code, as shown below, do we need some checks (an explicit comparison with $ null is the best):

 # we just try to get a new object $ie = $null try { $ie = New-Object -ComObject InternetExplorer.Application } catch { Write-Warning $_ } # check and continuation if ($ie -ne $null) { ... } 
+13
Dec 14 '10 at 17:35
source share

All of these answers do not highlight that when comparing a value with $ null, you must put $ null on the left, otherwise you may run into problems when comparing with the value of the collection type. See: https://github.com/nightroman/PowerShellTraps/blob/master/Basic/Comparison-operators-with-collections/looks-like-object-is-null.ps1

 $value = @(1, $null, 2, $null) if ($value -eq $null) { Write-Host "$value is $null" } 

The above block is completed (unfortunately). More interestingly, in Powershell, the value of $ can be both $ null and not $ null:

 $value = @(1, $null, 2, $null) if (($value -eq $null) -and ($value -ne $null)) { Write-Host "$value is both $null and not $null" } 

Therefore, it is important to put $ null on the left so that these comparisons work with collections:

 $value = @(1, $null, 2, $null) if (($null -eq $value) -and ($null -ne $value)) { Write-Host "$value is both $null and not $null" } 

I think this once again shows the power of Powershell!

+3
Mar 21 '18 at 16:31
source share

Type checking with the -is operator returns false for any null value. In most cases, if not all, $ value -is [System.Object] will be true for any possible non-zero value. (In all cases, it will be false for any zero value.)

My meaning means nothing, if not an object.

+1
Jan 12 '15 at 11:30
source share

I had the same problem. This solution works for me.

 $Word = $null $Word = [System.Runtime.InteropServices.Marshal]::GetActiveObject('word.application') if ($Word -eq $null) { $Word = new-object -ComObject word.application } 

https://msdn.microsoft.com/de-de/library/system.runtime.interopservices.marshal.getactiveobject(v=vs.110).aspx

0
Jul 10 '17 at 10:29 on
source share

In case you are like me, you have landed here, trying to find a way to determine if your PowerShell variable is this particular kind of nonexistent:

A COM object that has been separated from the base RCW cannot be used.

Then here is the code that worked for me:

 function Count-RCW([__ComObject]$ComObj){ try{$iuk = [System.Runtime.InteropServices.Marshal]::GetIUnknownForObject($ComObj)} catch{return 0} return [System.Runtime.InteropServices.Marshal]::Release($iuk)-1 } 

usage example:

 if((Count-RCW $ExcelApp) -gt 0){[System.Runtime.InteropServices.Marshal]::FinalReleaseComObject($ExcelApp)} 

glued from other people's best answers:

and some other interesting things:

0
Jun 12 '18 at 21:50
source share



All Articles