Export (0) Print
Expand All
Expand Minimize

New-Event

Updated: December 3, 2014

Applies To: Windows PowerShell 4.0

New-Event

Creates a new event.

Syntax

Parameter Set: Default
New-Event [-SourceIdentifier] <String> [[-Sender] <PSObject> ] [[-EventArguments] <PSObject[]> ] [[-MessageData] <PSObject> ] [ <CommonParameters>]




Detailed Description

The New-Event cmdlet creates a new custom event.

You can use custom events to notify users about state changes in your program and any change that your program can detect, including hardware or system conditions, application status, disk status, network status, or the completion of a background job.

Custom events are automatically added to the event queue in your session whenever they are raised; you do not need to subscribe to them. However, if you want to forward an event to the local session or specify an action to respond to the event, use the Register-EngineEvent cmdlet to subscribe to the custom event.

When you subscribe to a custom event, the event subscriber is added to your session. If you cancel the event subscription by using the Unregister-Event cmdlet, the event subscriber and custom event are deleted from the session. If you do not subscribe to the custom event, to delete the event, you must change the program conditions or close the Windows PowerShell session.

Parameters

-EventArguments<PSObject[]>

Specifies an object that contains options for the event.


Aliases

none

Required?

false

Position?

3

Default Value

None

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-MessageData<PSObject>

Specifies additional data associated with the event. The value of this parameter appears in the MessageData property of the event object.


Aliases

none

Required?

false

Position?

4

Default Value

None

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Sender<PSObject>

Specifies the object that raises the event. The default is the Windows PowerShell engine.


Aliases

none

Required?

false

Position?

2

Default Value

Windows PowerShell engine

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-SourceIdentifier<String>

Specifies a name for the new event. This parameter is required, and it must be unique in the session.

The value of this parameter appears in the SourceIdentifier property of the events.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see  about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • None

    You cannot pipe input to this cmdlet.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • System.Management.Automation.PSEventArgs

Notes

  • The new custom event, the event subscription, and the event queue exist only in the current session. If you close the current session, the event queue is discarded and the event subscription is canceled.

Examples

-------------------------- EXAMPLE 1 --------------------------

This command creates a new event in the Windows PowerShell event queue. It uses a Windows.Timer object to send the event.


PS C:\> new-event -sourceidentifier Timer -sender windows.timer  -messagedata "Test"

-------------------------- EXAMPLE 2 --------------------------

This sample function uses the New-Event cmdlet to raise an event in response to another event. The command uses the Register-ObjectEvent cmdlet to subscribe to the Windows Management Instrumentation (WMI) event that is raised when a new process is created. The command uses the Action parameter of the cmdlet to call the New-Event cmdlet, which creates the new event.

Because the events that New-Event raises are automatically added to the Windows PowerShell event queue, you do not need to register for that event.


PS C:\> function Enable-ProcessCreationEvent
{
   $query = New-Object System.Management.WqlEventQuery "__InstanceCreationEvent", (New-Object TimeSpan 0,0,1), "TargetInstance isa 'Win32_Process'"
   $processWatcher = New-Object System.Management.ManagementEventWatcher $query
   $identifier = "WMI.ProcessCreated"
   Register-ObjectEvent $processWatcher "EventArrived" -SupportEvent $identifier -Action 
   {
      [void] (New-Event -sourceID "PowerShell.ProcessCreated" -Sender $args[0] –EventArguments $args[1].SourceEventArgs.NewEvent.TargetInstance)
   }
}

Related topics



Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft