{"id":8971,"date":"2012-06-22T00:01:00","date_gmt":"2012-06-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/06\/22\/top-five-powershell-tasks-a-user-might-need-to-accomplish\/"},"modified":"2012-06-22T00:01:00","modified_gmt":"2012-06-22T00:01:00","slug":"top-five-powershell-tasks-a-user-might-need-to-accomplish","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/top-five-powershell-tasks-a-user-might-need-to-accomplish\/","title":{"rendered":"Top Five PowerShell Tasks a User Might Need to Accomplish"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, discusses the top five things a normal user might need to use Windows PowerShell to do.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! What good is Windows PowerShell to a normal Windows User?<\/p>\n<p>&mdash;GK<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello GK,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This afternoon begins to stretch me into normalcy. I completed my TechEd 2012 Trip Report, attended a couple of meetings, and made a pot of Gun Powder Green Tea with a small amount of organic hibiscus flower in it. Along with a piece of 90 percent cacao, it was a perfect afternoon. GK, as I began to catch up on my <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a> email, I ran across your letter. It reminds me of something the Scripting Wife says to me when I am presenting at SQL Saturday, &ldquo;Tell them what good it is.&rdquo; So here are the five top tasks that I like Windows PowerShell to do for me&hellip;<\/p>\n<p><strong>1.&nbsp;<\/strong><b>Close a bunch of copies of a program at once.<\/b> It never seems to fail, I click a program, wait for a few minutes, and nothing seems to happen. Therefore, I click the icon again&hellip;and again&hellip;and again. All of a sudden, I hear the fan on my laptop kick into overdrive, I see the hard disk drive activity light go crazy, and all at once, my display becomes covered with open windows from the wayward application. <br \/> If you are like me, you know how to launch Task Manager by selecting &ldquo;Start Task Manager&rdquo; from the action menu of the task bar or by pressing &lt;Ctrl&gt;&lt;Shift&gt;&lt;Esc&gt; all at once. But the problem is that Task Manager does not support multiple select; and therefore, it requires multiple clicks and selections prior to deleting all copies of the wayward application. Task Manager is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3482.hsg-6-22-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3482.hsg-6-22-12-01.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>The solution is to use the <b>Get-Process<\/b> cmdlet and the <b>Stop-Process<\/b> cmdlet. You might want to see if you can identify the number of copies of the process. To do this, use the <b>Get-Process<\/b> cmdlet as shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Process iexplore<\/p>\n<p>Before attempting such a solution, it is a good idea to test the solution by using the <i>&ndash;WhatIf<\/i> switch as shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Process iexplore | Stop-Process &ndash;WhatIf<\/p>\n<p>When you have found that it does what you want, use the Up arrow to recall the command and remove the <i>&ndash;WhatIf<\/i>. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Process iexplore | Stop-Process<\/p>\n<p>No output displays from the command. The previous commands and the output associated with them are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4431.hsg-6-22-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4431.hsg-6-22-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><strong>2.<\/strong><b> Find the date <\/b>for a month from now, or at some time in the past, or any time in the future. Often I need to find what the date will be for 30 days or 60 days in the future. To do this, I use the <b>Get-Date<\/b> cmdlet. The trick is to use the <b>adddays <\/b>method. To do this, I use the <b>Get-Date<\/b> cmdlet, put parentheses around it, use a dot, and call the <b>adddays <\/b>method. Luckily, <b>adddays <\/b>accepts negative numbers, which is great for going back in time. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; (Get-Date).adddays(30)&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Thursday, July 19, 2012 4:34:40 PM&nbsp;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; (Get-Date).adddays(-30)<\/p>\n<p style=\"padding-left: 30px\">Sunday, May 20, 2012 4:34:46 PM&nbsp;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt;<\/p>\n<p><strong>3.<\/strong><b> Find the most recent entry in the Application log.<\/b> One of the cool things about Windows&nbsp;7 is the new event logs. One of the bad things about Windows&nbsp;7 is all of the new logs. It takes a bit of time for the Event Viewer to open to allow you to select a specific log and look at it. By using Windows PowerShell, that task is a breeze. Want to? No problem. It is a single command, as shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-EventLog application -Newest 1<\/p>\n<p>Or how about finding all of the entries from the system log that occurred after midnight on June 19, 2012? This, too, is an easy task. The command is shown here.&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Get-EventLog system&nbsp; -After 6\/19\/2012&nbsp;<\/p>\n<p><strong>4<\/strong>. <strong>Find a list of all the services on your computer that are running or stopped<\/strong>. By using Windows PowerShell, this is a piece-of-cake.<\/p>\n<p style=\"padding-left: 30px\">Get-Service | where {$_.status -eq &#8216;running&#8217;}<\/p>\n<p style=\"padding-left: 30px\">Get-Service | where {$_.status -eq &#8216;stopped&#8217;}<\/p>\n<p>In Windows PowerShell&nbsp;3.0, the command is easier to read because of the new simplified syntax. The revised command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Service | where status -eq &#8216;stopped&#8217;<\/p>\n<p style=\"padding-left: 30px\">Get-Service | where status -eq &#8216;running&#8217;<\/p>\n<p><strong>5.<\/strong><b> Shut down my computer.<\/b> When I am finished for the day, I use Windows PowerShell to shut down my computer. The command, <b>Stop-Computer<\/b>, is easy-to-use as shown here.<\/p>\n<p style=\"padding-left: 30px\">Stop-Computer<\/p>\n<p>I can also use this to shut down multiple computers from across the network because the <i>&ndash;ComputerName<\/i> parameter accepts an array of computer names. The following command illustrates this technique. This is also a good command to use the <i>&ndash;WhatIf<\/i> parameter with.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Stop-Computer -ComputerName dc1,dc3 -WhatIf<\/p>\n<p style=\"padding-left: 30px\">What if: Performing operation &#8220;Stop-Computer&#8221; on Target &#8221; (dc1)&#8221;.<\/p>\n<p style=\"padding-left: 30px\">What if: Performing operation &#8220;Stop-Computer&#8221; on Target &#8221; (dc3)&#8221;.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt;<\/p>\n<p>When you are certain you want to perform the action, use the Up arrow and remove the <em>&ndash;WhatIf<\/em> parameter. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Stop-Computer -ComputerName dc1,dc3<\/p>\n<p>GK, those are my five favorite tasks to accomplish with Windows PowerShell. No doubt others will have other suggestions and post them as comments to the posting. Join me tomorrow for more Windows PowerShell cool stuff.<\/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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, discusses the top five things a normal user might need to use Windows PowerShell to do. &nbsp;Hey, Scripting Guy! What good is Windows PowerShell to a normal Windows User? &mdash;GK &nbsp;Hello GK, Microsoft Scripting Guy, Ed Wilson, is here. This afternoon begins to stretch me into normalcy. I completed [&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":[229,51,3,4,45],"class_list":["post-8971","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-beginner","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, discusses the top five things a normal user might need to use Windows PowerShell to do. &nbsp;Hey, Scripting Guy! What good is Windows PowerShell to a normal Windows User? &mdash;GK &nbsp;Hello GK, Microsoft Scripting Guy, Ed Wilson, is here. This afternoon begins to stretch me into normalcy. I completed [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8971","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=8971"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8971\/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=8971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}