{"id":54583,"date":"2009-01-19T20:57:00","date_gmt":"2009-01-19T20:57:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/01\/19\/hey-scripting-guy-how-can-i-be-notified-when-a-process-begins\/"},"modified":"2009-01-19T20:57:00","modified_gmt":"2009-01-19T20:57:00","slug":"hey-scripting-guy-how-can-i-be-notified-when-a-process-begins","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-be-notified-when-a-process-begins\/","title":{"rendered":"Hey, Scripting Guy! How Can I Be Notified When a Process Begins?"},"content":{"rendered":"<h2><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/h2>\n<p>Hey, Scripting Guy! I need to be notified when a process begins. I would like to use Windows PowerShell to do this, but it does not seem to work. While I could do this in VBScript, I prefer to use Windows PowerShell. Can this be done?<\/p>\n<p>&#8211; NL<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><\/p>\n<p>Hi NL,<\/p>\n<p>Because we have access to the .NET Framework in Windows PowerShell, it can most certainly be done. In Windows PowerShell 2.0, which just released as Community Technology Preview (CTP) 3, there is actually a cmdlet that you can use. But because a CTP is not supported, you can use the .NET Framework classes today. The advantage of this approach is that it will work with both Windows PowerShell 1.0 and Windows PowerShell 2.0, when it ships with Windows 7.<\/p>\n<table class=\"dataTable\" id=\"EXC\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">This week we will be talking about WMI eventing. For some VBScript examples of these kinds of scripts, you can refer to the eventing section of the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/events.mspx\">Hey, Scripting Guy! archive<\/a>. Additional information can be obtained from <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_wmi_kzcp.mspx?mfr=true\">this Web page<\/a>. The Microsoft book, <a href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth8853.aspx\">Microsoft Windows Scripting with WMI: Self-Paced Learning Edition<\/a>, has an entire chapter on WMI eventing.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Today&#8217;s script, <b>MonitorForProcessStartUp.ps1<\/b>, obtains diagnostic process information when any new process starts up. Here it is:<\/p>\n<pre class=\"codeSample\">Clear-Host\n$Start = get-date\nWrite-Host \"Waiting for a new process to be created ...\nYou will be notified within 10 seconds of  process  creation\n start time was $Start\"\n$Query = \"select * from __instanceCreationEvent within 10\n          where targetInstance isa 'win32_Process'\"\n$Eventwatcher = New-Object management.managementEventWatcher $Query\n$Event = $Eventwatcher.waitForNextEvent()\n$Event.targetInstance |\nFormat-List -property [a-z]*\n<\/pre>\n<p>The first thing we want to do is to clear the screen. We do this so we can take a nice screen shot for today&#8217;s article, but it is also good for telling at a glance if your event has fired. As a best practice, you should actually prompt the user before clearing the screen. This is because someone could have data on the screen of their Windows PowerShell console, and become really enraged if you were to erase the screen. Personally, I have never seen that situation, but theoretically it could happen. To clear the screen you use the <b>Clear-Host<\/b> function as seen here:<\/p>\n<pre class=\"codeSample\">Clear-Host<\/pre>\n<p>If you would like to prompt the user before clearing the screen, you could use this&nbsp; technique:<\/p>\n<pre class=\"codeSample\">$response = Read-Host -prompt \"Would you like to clear the screen? &amp;lt;yes&amp;gt;&amp;lt;no&amp;gt;\"\nif($response -eq \"yes\") { clear-host }\n<\/pre>\n<p>It is time to get the time, or the entire date as is our case here. To get a time stamp, we use the <b>Get-Date<\/b> cmdlet. The <b>Get-Date<\/b> cmdlet returns an entire <b>System.DateTime<\/b> object, which we store in the <b>$Start<\/b> variable. You may not be too excited about <b>DateTime<\/b> objects, but to me they are one of the strongest motivators for using Windows PowerShell instead of using VBScript. Why? Well, I hated date manipulation in VBScript\u2014just a <a href=\"http:\/\/encarta.msn.com\/dictionary_1861725367\/pet_peeve.html\">pet peeve<\/a> perhaps. (Pet peeves are pretty cool. T hey do not make much noise, do not make messes, and eat very little. I have never had a pet peeve get me up in the middle of the night to take it outside either.) The members of the <b>System.DateTime<\/b> object are seen in Table 1. <\/p>\n<p>Table 1 Members of the System.DateTime object<\/p>\n<table class=\"dataTable\" id=\"ETE\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead>\n<tr class=\"stdHeader\" vAlign=\"top\">\n<td class=\"\" id=\"colEVE\">Name<\/td>\n<td class=\"\" id=\"colEZE\">Member Type<\/td>\n<td class=\"\" id=\"colE4E\">Definition<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Add<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime Add(TimeSpan value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">AddDays<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime AddDays(Double value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">AddHours<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime AddHours(Double value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">AddMilliseconds<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime AddMilliseconds(Double value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">AddMinutes<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime AddMinutes(Double value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">AddMonths<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime AddMonths(Int32 months)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">AddSeconds<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime AddSeconds(Double value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">AddTicks<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime AddTicks(Int64 value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">AddYears<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime AddYears(Int32 value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">CompareTo<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int32 CompareTo(Object value), System.Int32 CompareTo(DateTime value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Equals<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Boolean Equals(Object value), System.Boolean Equals(DateTime value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">GetDateTimeFormats<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.String[] GetDateTimeFormats(), System.String[] GetDateTimeFormats(IFormatProvider provider), System.String[] GetDateTimeFormats(Char format), System.String[] GetDateTimeFormats(Char format, IFormatProvider provider)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">GetHashCode<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int32 GetHashCode()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">GetType<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Type GetType()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">GetTypeCode<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.TypeCode GetTypeCode()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">IsDaylightSavingTime<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Boolean IsDaylightSavingTime()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Subtract<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.TimeSpan Subtract(DateTime value), System.DateTime Subtract(TimeSpan value)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToBinary<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int64 ToBinary()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToFileTime<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int64 ToFileTime()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToFileTimeUtc<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int64 ToFileTimeUtc()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToLocalTime<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime ToLocalTime()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToLongDateString<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.String ToLongDateString()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToLongTimeString<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.String ToLongTimeString()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToOADate<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Double ToOADate()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToShortDateString<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.String ToShortDateString()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToShortTimeString<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.String ToShortTimeString()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToString<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.String ToString(), System.String ToString(String format), System.String ToString(IFormatProvider provider), System.String ToString(String format, IFormatProvider provider)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">ToUniversalTime<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime ToUniversalTime()<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">DisplayHint<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">NoteProperty<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Microsoft.PowerShell.Commands.DisplayHintType DisplayHint=DateTime<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Date<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTime Date {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Day<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int32 Day {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">DayOfWeek<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DayOfWeek DayOfWeek {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">DayOfYear<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int32 DayOfYear {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Hour<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int32 Hour {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Kind<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.DateTimeKind Kind {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Millisecond<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int32 Millisecond {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Minute<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int32 Minute {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Month<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int32 Month {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Second<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int32 Second {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Ticks<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int64 Ticks {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">TimeOfDay<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.TimeSpan TimeOfDay {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Year<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Property<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">System.Int32 Year {get;}<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">DateTime<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">ScriptProperty<\/p>\n<\/td>\n<td class=\"\">\n<pre class=\"codeSample\"><pre class=\"codeSample\">System.Object DateTime {get=if ($this.DisplayHint -ieq  Date) {\n                        {0} -f $this.ToLongDateString()\n                    }\n                    elseif ($this.DisplayHint -ieq Time)\n                    {\n                        {0} -f  $this.ToLongTimeString()\n                    }\n                    else\n                    {\n                        {0} {1} -f $this.ToLongDateString(), $this.ToLongTimeString()\n                    };}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>To store the start date in the <b>$Start<\/b> variable, we use this code:<\/p>\n<pre class=\"codeSample\">$Start = get-date<\/pre>\n<p>We now need to prompt the user to let them know that the script is actually working. We can use the <b>Write-Host<\/b> cmdlet to do this. We use an expanding string, which allows us to use the <b>datetime<\/b> value stored in the <b>$Start<\/b> variable directly. We do not need to fool with concatenation. Strings in Windows PowerShell are assumed to continue to the next line if they are not closed, and we can break our message up across three lines without needing to use line concatenation, or even embedding carriage returns and line feeds (a la <b>vbcrlf<\/b> from VBScript fame). This is seen here: <\/p>\n<pre class=\"codeSample\">Write-Host \"Waiting for a new process to be created ...\nYou will be notified within 10 seconds of  process  creation\n start time was $Start\"\n<\/pre>\n<p>When the script is run, the message is printed on the screen along with the current starting time. This is shown here:<\/p>\n<p><img decoding=\"async\" height=\"409\" alt=\"Image of the message printed on the screen when the script is run\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/january\/hey0119\/HSG_1_19_200901.jpg\" width=\"500\" border=\"0\"><\/p>\n<p>&nbsp;<\/p>\n<p>After we have prompted our user, we can create our event query. This uses the same query syntax familiar from <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_wmi_kzcp.mspx?mfr=true\">VBScript event scripts<\/a>. This query string is seen here: <\/p>\n<pre class=\"codeSample\">$Query = \"select * from __instanceCreationEvent within 10\n          where targetInstance isa 'win32_Process'\"\n<\/pre>\n<p>Now we need to actually create the event watcher. To do this, we first need to create an instance of the <b>System.Management.ManagementEventWatcher<\/b> .NET Framework class. We use the <b>New-Object<\/b> cmdlet to do this. The class name is <b>ManagementEventWatcher<\/b>, and the namespace the class is found in is <b>System.Management<\/b>. Because we need to know the namespace the class resides in to be able to create an instance of the class, I make it a habit of always referring to the class by the full name. It is a bit cumbersome, but I have found it to be much more convenient than spending 15 minutes searching MSDN documentation to try to identify the namespace of a class. When we create the instance of the <b>ManagementEventWatcher<\/b>, we give it a constructor. The constructor is used to govern the way in which the class is actually created. In this example, we are giving <b>ManagementEventWatcher<\/b> the query stored in the <b>$query<\/b> parameter. This is my favorite constructor for this class. Other constructors are listed on <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.management.managementeventwatcher.managementeventwatcher.aspx\">MSDN<\/a>. We store the newly minted <b>System.Management.ManagementEventWatcher<\/b> class in the <b>$EventWatcher<\/b> variable as seen here:<\/p>\n<pre class=\"codeSample\">$Eventwatcher = New-Object system.management.managementEventWatcher $Query<\/pre>\n<p>Now all we need to do is to sit and wait. We are using the <b>WaitForNextEvent<\/b> method from the <b>System.Management.ManagementEventWatcher<\/b> class. When the event occurs, the returned <b>ManagementBaseObject<\/b> is stored in the <b>$event<\/b> variable as seen here:<\/p>\n<pre class=\"codeSample\">$Event = $Eventwatcher.waitForNextEvent()<\/pre>\n<p>Next we use the <b>targetInstance<\/b> property to retrieve the properties of the <b>Win32_Process<\/b> class. We pipeline this to the <b>Format-List<\/b> cmdlet and print out all the properties that begin with the letters <b>a-z<\/b> and are followed by any other character. This removes the system properties that begin with the double underscore (such as <b>__NameSpace<\/b>). This is seen here: <\/p>\n<pre class=\"codeSample\">$Event.targetInstance |\nFormat-List -property [a-z]*\n<\/pre>\n<p>The result is all the information normally associated with a query to the <b>Win32_Process<\/b> WMI class. This is shown here:<\/p>\n<p><img decoding=\"async\" height=\"409\" alt=\"Image of the information in the notification\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/january\/hey0119\/HSG_1_19_200902.jpg\" width=\"500\" border=\"0\"><\/p>\n<p>&nbsp;<\/p>\n<p>Well, NL, we hope you enjoyed today&#8217;s article about monitoring for process startup. We will continue working with WMI events tomorrow. Until then, do not pick up any stray pet peeves you see wandering along the side of the road. See you tomorrow.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need to be notified when a process begins. I would like to use Windows PowerShell to do this, but it does not seem to work. While I could do this in VBScript, I prefer to use Windows PowerShell. Can this be done? &#8211; NL Hi NL, Because we have access to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[42,31,87,3,4,45,6],"class_list":["post-54583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-events-and-monitoring","tag-operating-system","tag-processes","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need to be notified when a process begins. I would like to use Windows PowerShell to do this, but it does not seem to work. While I could do this in VBScript, I prefer to use Windows PowerShell. Can this be done? &#8211; NL Hi NL, Because we have access to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=54583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54583\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=54583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}