How to access an indexed property on a COM object from Powershell

Question:

I’m using Powershell to talk to the Windows 7 task scheduler service via COM through the Task Scheduler 2.0 interfaces (e.g. ITaskDefinition). I want to pull out a particular trigger from the Triggers collection on ITaskDefinition. It appears that the proper way to extract a particular trigger is through the Item property, which is an indexed property.

My first try looks something like this:

However, the last line fails with this message:

I’ve tried some other variations on this theme, e.g. $triggers.Item(0), all with no luck. I’m guessing this has to do with $trigger being a COM object, because I think indexed properties work fine on other types.

Does anyone know the correct way to do this?

Answer:

Item does work. You have to use Item() instead of Item[] and indices are 1-based.

Using an enumerator also works if you don’t need to access by index:

Source:

How to access an indexed property on a COM object from Powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply