How to generate the custom event from script.

Hi, there!

I have read the documentation about PowerShell events cmndlets (New-Event, New-ObjectEvent, Register-EngineEvent, etc.), but there no clear information about this kind of task: for example I need to raise the event (to write “It’s reached”), when some code is reach some value:

for($i=0; $i -gt 10; $i++) {

if($i -eq 5) { #HERE THE CODE FOR GENERATE EVENT - ??? }


I couldn’t find the similar code example through the Google, so may somebody to provide the code example for generate of this type event ?


Ah, speaking of clear information, perhaps you could give a bit more detail on what you want to do.

If you just want the script to write “It’s reached” to the screen when a condition is met, it’s as simple as this:

if ($i -eq 5) {echo "It's reached"}

and in fact you don’t even need ‘echo’, so it can just be:

if ($i -eq 5) {"It's reached"}

Why do you need an event, what is the event supposed to trigger, and what is handling the event when it occurs?

Also, please format your code with pre tags or by using a github gist, as per this post.


You’re right.

Perhaps it’s not clear task and I don’t need to use events here - sorry. I’ll try to think better on this task.