{"id":7431,"date":"2015-03-07T00:01:00","date_gmt":"2015-03-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/07\/weekend-scripter-avoid-powershell-scriptinguse-gui-tools\/"},"modified":"2019-02-18T10:30:22","modified_gmt":"2019-02-18T17:30:22","slug":"weekend-scripter-avoid-powershell-scriptinguse-gui-tools","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-avoid-powershell-scriptinguse-gui-tools\/","title":{"rendered":"Weekend Scripter: Avoid PowerShell Scripting&#8212;Use GUI Tools"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about exporting queries from the event log tool.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It has long been a truism (at least with things related to computers): powerful is opposite of simple. I can have a tool that is powerful, but with that power comes complexity. When that complexity is so complex as to render the tool ridiculously hard to use, the tool rapidly becomes useless.<\/p>\n<p>What is awesome is when a tool is extremely powerful and also very easy to use. Of course, this usually means that the tool makes lots of default choices for me. If those default choices are intelligent, I really don&rsquo;t care. Microsoft Word is sort of like that. I mean, the default document template makes tons of choices. Usually, I do not care about the exact spacing between paragraphs, the default Tab stop, column width, or page length. Usually&hellip;<\/p>\n<p>Of course, when I have to modify those things, I know I am probably going to have to set aside all day.<\/p>\n<p>The Windows PowerShell cmdlet <b>Get-WinEvent<\/b> is often perceived to be such a tool. It is way powerful&mdash;but it is also more complicated to use, than for example, the <b>Get-EventLog<\/b> cmdlet. The problem with <b>Get-EventLog<\/b> is that it only works for legacy event logs. For all the newer (new as in Windows Vista era&mdash;so not really all that new at all) types of logs, I need to use <b>Get-WinEvent<\/b>. Because <b>Get-WinEvent<\/b> also works with legacy event logs, I have completely quit using the <b>Get-EventLog<\/b> cmdlet. This forces me to learn how to use the <b>Get-WinEvent<\/b> cmdlet.<\/p>\n<p>One problem with the <b>Get-WinEvent<\/b> cmdlet, is at first glance, it is hard to figure out how to filter the results. It is a truism, that for performance sake, I filter to the left of the pipeline character. So this means that I do not use <b>Get-WinEvent<\/b> to return everything and then pipe it to the <b>Where-Object<\/b>.<\/p>\n<p>This is especially true with some logs that return thousands of records. But how do I filter, for example, on an Event ID? Here is the syntax that shows the various parameter sets (ways of using the cmdlet):<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-Command Get-WinEvent -Syntax<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Get-WinEvent [[-LogName] &lt;string[]&gt;] [-MaxEvents &lt;long&gt;] [-ComputerName &lt;string&gt;] [-Credential<\/p>\n<p style=\"margin-left:30px\">&lt;pscredential&gt;] [-FilterXPath &lt;string&gt;] [-Force] [-Oldest] [&lt;CommonParameters&gt;]<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Get-WinEvent [-ListLog] &lt;string[]&gt; [-ComputerName &lt;string&gt;] [-Credential &lt;pscredential&gt;] [-Force]<\/p>\n<p style=\"margin-left:30px\">[&lt;CommonParameters&gt;]<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Get-WinEvent [-ListProvider] &lt;string[]&gt; [-ComputerName &lt;string&gt;] [-Credential &lt;pscredential&gt;]<\/p>\n<p style=\"margin-left:30px\">[&lt;CommonParameters&gt;]<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Get-WinEvent [-ProviderName] &lt;string[]&gt; [-MaxEvents &lt;long&gt;] [-ComputerName &lt;string&gt;] [-Credential<\/p>\n<p style=\"margin-left:30px\">&lt;pscredential&gt;] [-FilterXPath &lt;string&gt;] [-Force] [-Oldest] [&lt;CommonParameters&gt;]<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Get-WinEvent [-Path] &lt;string[]&gt; [-MaxEvents &lt;long&gt;] [-Credential &lt;pscredential&gt;] [-FilterXPath<\/p>\n<p style=\"margin-left:30px\">&lt;string&gt;] [-Oldest] [&lt;CommonParameters&gt;]<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Get-WinEvent [-FilterXml] &lt;xml&gt; [-MaxEvents &lt;long&gt;] [-ComputerName &lt;string&gt;] [-Credential<\/p>\n<p style=\"margin-left:30px\">&lt;pscredential&gt;] [-Oldest] [&lt;CommonParameters&gt;]<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Get-WinEvent [-FilterHashtable] &lt;hashtable[]&gt; [-MaxEvents &lt;long&gt;] [-ComputerName &lt;string&gt;]<\/p>\n<p style=\"margin-left:30px\">[-Credential &lt;pscredential&gt;] [-Force] [-Oldest] [&lt;CommonParameters&gt;]<\/p>\n<p>From this, there are basically three ways of filtering:<\/p>\n<ul>\n<li>Via XML<\/li>\n<li>Via a hash table<\/li>\n<li>Via XPath<\/li>\n<\/ul>\n<p>Dude!!!<\/p>\n<h2>The easy way to an XPath query<\/h2>\n<p>Believe it or not, the easy way to filter the results of <b>Get-WinEvent<\/b> is with XPath. This is because I can use the Event Viewer to create my query for me. To do this, I open the Event Viewer, right-click the log, and choose <b>Filter Current Log<\/b> from the action menu. I then use the check boxes, drop-down lists, and text boxes to filter the content of the selected log. This is shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-7-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-7-15-01.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>I then click the <b>XML<\/b> tab to look at the query. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-7-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-7-15-02.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Now I select the <b>Edit query manually<\/b> check box. This permits me to highlight the query. I always copy the query and paste it into Notepad. When I have the query in Notepad, I select the <b>Path<\/b> portion of the query:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-7-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-7-15-03.png\" alt=\"Image of command\" title=\"Image of command\" \/><\/a><\/p>\n<p>I open the Windows PowerShell ISE, create my query, and add a line for my <b>Get-WinEvent<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">$xpath = &quot;*[System[(Level=2) and (EventID=35)]]&quot;<\/p>\n<p style=\"margin-left:30px\">Get-WinEvent -LogName application -FilterXPath $xpath&nbsp;<\/p>\n<p>And that is the easy way to query a log by using the <b>Get-WinEvent<\/b> cmdlet.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about exporting queries from the event log tool. Microsoft Scripting Guy, Ed Wilson, is here. It has long been a truism (at least with things related to computers): powerful is opposite of simple. I can have a tool that is powerful, but with that power comes complexity. When [&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":[42,3,4,61,45],"class_list":["post-7431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-events-and-monitoring","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about exporting queries from the event log tool. Microsoft Scripting Guy, Ed Wilson, is here. It has long been a truism (at least with things related to computers): powerful is opposite of simple. I can have a tool that is powerful, but with that power comes complexity. When [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7431","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=7431"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7431\/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=7431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}