{"id":12091,"date":"2011-11-14T00:01:00","date_gmt":"2011-11-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/11\/14\/use-custom-views-from-windows-event-viewer-in-powershell\/"},"modified":"2011-11-14T00:01:00","modified_gmt":"2011-11-14T00:01:00","slug":"use-custom-views-from-windows-event-viewer-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-custom-views-from-windows-event-viewer-in-powershell\/","title":{"rendered":"Use Custom Views from Windows Event Viewer in PowerShell"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Learn how to use Event Viewer custom views in Windows PowerShell to parse event logs quickly.<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hey, Scripting Guy! I love Windows 7. It absolutely rocks! One of the things I love about Windows 7, in addition to Windows PowerShell, is the new Event Viewer. I have created a custom view in my Event Viewer. I exported that custom view, and when I try to use it in the <b>Get-WinEvent<\/b> cmdlet, it fails. Can you help me? I would love to be able to use Windows PowerShell to parse my custom view of the event logs. I know you can do this because you are the greatest!<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&mdash;LD<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hello LD, <\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Microsoft Scripting Guy Ed Wilson here. I agree with you, at least on two counts. I also love Windows PowerShell 7 and the new Event Viewer. The Windows PowerShell <b>Get-WinEvent<\/b> cmdlet is also very powerful, and provides lots of opportunities for experimentation. I have written many articles about <\/span><a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=Get-WinEvent&amp;sections=7618\">using the <b>Get-WinEvent<\/b> cmdlet<\/a><span style=\"font-family: Segoe\"> on the Hey, Scripting Guy! Blog. <\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">So, let&rsquo;s see what exactly you are talking about when it comes to exporting a custom view from the Event Viewer application. As shown in the following figure, when I open the Event Viewer, the top portion in the upper left section of the screen contains Custom Views<i>.<\/i> <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0576.hsg-11-14-11-1.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of Custom Views in Event Viewer\" alt=\"Image of Custom Views in Event Viewer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0576.hsg-11-14-11-1.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">To create a custom view, I select <b>Create Custom View<\/b><i> <\/i>from the <b>Action<\/b> pane and the <b>Create Custom View<\/b><i> <\/i>interface is displayed. This dialog box is shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8787.hsg-11-14-11-2.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of Create Custom View dialog box\" alt=\"Image of Create Custom View dialog box\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8787.hsg-11-14-11-2.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">After I save the custom view, I can export it to XML by selecting the custom view, and clicking <b>Export Custom View<\/b><i> <\/i>in the <b>Action<\/b> menu. This technique works great for exporting custom event log views either for backup purposes, or to use on other computers via the Event Viewer application. Unfortunately, it does not work when I attempt to import it via the <b>Get-WinEvent<\/b> cmdlet:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-WinEvent -FilterXml ([xml](Get-Content C:\\fso\\exportedCustomView.xml))<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command and associated error are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7701.hsg-11-14-11-3.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated error\" alt=\"Image of command and associated error\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7701.hsg-11-14-11-3.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The reason the error is generated is because <b>Export Custom View<\/b><i> <\/i>includes additional information required by the Event Viewer to create and host the custom event view. What the <b>Get-WinEvent<\/b> cmdlet requires is the <b>&lt;QueryList&gt;<\/b> information. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">To find the <b>&lt;QueryList&gt;<\/b> information, I click <b>Filter Current Custom View<\/b><i> <\/i>in the <b>Action<\/b> menu. When the <b>Filter Current Custom View<\/b> dialog box appears, I click the <b>XML<\/b> tab. This displays the <b>&lt;QueryList&gt;<\/b> information, as shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6320.hsg-11-14-11-4.png\">&lt;img style=&quot;border: 0px;&quot; title=&quot;Image of  information&#8221; alt=&#8221;Image of  information&#8221; src=&#8221;https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6320.hsg-11-14-11-4.png&#8221; original-url=&#8221;http:\/\/blogs.technet.com\/resized-image.ashx\/__size\/550&#215;0\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/6320.hsg_2D00_11_2D00_14_2D00_11_2D00_4.png&#8221; \/&gt;<\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">There is no copy button, even if you select the <b>Edit query manually<\/b> check box. But I can easily highlight everything with my mouse, and press Ctrl+C to copy the selection to the Clipboard. After I have copied the information to the Clipboard, I create a new text file, paste the contents, and save it with a .xml file extension. The following figure shows the contents of the custom event log view.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3731.hsg-11-14-11-5.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of contents of custom event log view\" alt=\"Image of contents of custom event log view\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3731.hsg-11-14-11-5.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">After I have only the <b>&lt;QueryList&gt;<\/b> information in a text file, I can now use the exact same command I used previously, but this time it works. The command I use is shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-WinEvent -FilterXml ([xml](Get-Content C:\\fso\\Past24CustomView.xml))<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6014.hsg-11-14-11-6.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6014.hsg-11-14-11-6.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Here are the steps I use:<\/span><\/p>\n<ol>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Create a custom view in the Event Viewer utility.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Display the <b>&lt;QueryList&gt;<\/b> information from the custom view by clicking <b>Filter Custom View<\/b> from in the <b>Action<\/b> menu.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Click the <b>XML<\/b> tab.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Highlight the <b>&lt;QueryList&gt;<\/b> information with your mouse, and press Ctrl+C to copy the <b>&lt;QueryList&gt;<\/b> data to the Clipboard.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Open Notepad and paste the information from the Clipboard into the new text file.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Save the file with a .xml file extension.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Use the <b>Get-Content<\/b> cmdlet to read the contents of the XML file.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Cast the returned data to <b>{XML]<\/b> type and pass it to the <i>FilterXML <\/i>parameter of the <b>Get-WinEvent<\/b> cmdlet.<\/span><\/span><\/li>\n<\/ol>\n<p><span style=\"font-family: Segoe;font-size: small\">That is it. It seems like a lot of steps, but they are pretty logical. In addition, this provides an excellent way to process data quickly from multiple event logs. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">LD, that is all there is to using a custom view from Event Viewer in a Windows PowerShell cmdlet. Join me tomorrow for more exciting Windows PowerShell tricks. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a><span style=\"font-family: Segoe\"> and <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a><span style=\"font-family: Segoe\">. If you have any questions, send email to me at <\/span><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a><span style=\"font-family: Segoe\">, or post your questions on the <\/span><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-size: small\"><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><\/span>&nbsp;<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Event Viewer custom views in Windows PowerShell to parse event logs quickly. &nbsp; Hey, Scripting Guy! I love Windows 7. It absolutely rocks! One of the things I love about Windows 7, in addition to Windows PowerShell, is the new Event Viewer. I have created a custom view in my [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[97,98,3,45],"class_list":["post-12091","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-event-logs","tag-logs-and-monitoring","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Event Viewer custom views in Windows PowerShell to parse event logs quickly. &nbsp; Hey, Scripting Guy! I love Windows 7. It absolutely rocks! One of the things I love about Windows 7, in addition to Windows PowerShell, is the new Event Viewer. I have created a custom view in my [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12091","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12091"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12091\/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=12091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}