4

I am creating a new com object to open a popup message

$a = new-object -comobject wscript.shell
$b = $a.popup(“WARNING“,0,”Box title”,0 + 0x30)

I noticed that the object remained opened all the time. Should I need to release it? The commands below will do the job?

$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($a)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
1

1 Answer 1

3

Yes, that is good practice. By releasing the com object, it is marked for cleanup to be destroyed by the garbage collector.

Actually, the reference counter of the object is decreased and if this reaches zero, the object is removed from memory when the garbage collector runs. You cannot control when that happens, but this way you make sure the object gets removed. See here

Sign up to request clarification or add additional context in comments.

2 Comments

So if I understand you every time I need to release it exactly as I wrote?
@Bandit Well, eventually the object would no longer be referenced when you exit PowerShell, but i always do it to avoid running into memory shortage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.