Top Five PowerShell Tasks a User Might Need to Accomplish

Top Five PowerShell Tasks a User Might Need to Accomplish

  • Comments 3
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, discusses the top five things a normal user might need to use Windows PowerShell to do.

Hey, Scripting Guy! Question Hey, Scripting Guy! What good is Windows PowerShell to a normal Windows User?

—GK

Hey, Scripting Guy! Answer Hello GK,

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 scripter@microsoft.com email, I ran across your letter. It reminds me of something the Scripting Wife says to me when I am presenting at SQL Saturday, “Tell them what good it is.” So here are the five top tasks that I like Windows PowerShell to do for me…

1. Close a bunch of copies of a program at once. 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…and again…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.
If you are like me, you know how to launch Task Manager by selecting “Start Task Manager” from the action menu of the task bar or by pressing <Ctrl><Shift><Esc> 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.

Image of menu

The solution is to use the Get-Process cmdlet and the Stop-Process cmdlet. You might want to see if you can identify the number of copies of the process. To do this, use the Get-Process cmdlet as shown here.

Get-Process iexplore

Before attempting such a solution, it is a good idea to test the solution by using the –WhatIf switch as shown here.

Get-Process iexplore | Stop-Process –WhatIf

When you have found that it does what you want, use the Up arrow to recall the command and remove the –WhatIf. This is shown here.

Get-Process iexplore | Stop-Process

No output displays from the command. The previous commands and the output associated with them are shown in the image that follows.

Image of command output

2. Find the date 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 Get-Date cmdlet. The trick is to use the adddays method. To do this, I use the Get-Date cmdlet, put parentheses around it, use a dot, and call the adddays method. Luckily, adddays accepts negative numbers, which is great for going back in time. This technique is shown here.

PS C:\> (Get-Date).adddays(30) 

Thursday, July 19, 2012 4:34:40 PM 

PS C:\> (Get-Date).adddays(-30)

Sunday, May 20, 2012 4:34:46 PM 

PS C:\>

3. Find the most recent entry in the Application log. One of the cool things about Windows 7 is the new event logs. One of the bad things about Windows 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.

Get-EventLog application -Newest 1

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. 

Get-EventLog system  -After 6/19/2012 

4. Find a list of all the services on your computer that are running or stopped. By using Windows PowerShell, this is a piece-of-cake.

Get-Service | where {$_.status -eq 'running'}

Get-Service | where {$_.status -eq 'stopped'}

In Windows PowerShell 3.0, the command is easier to read because of the new simplified syntax. The revised command is shown here.

Get-Service | where status -eq 'stopped'

Get-Service | where status -eq 'running'

5. Shut down my computer. When I am finished for the day, I use Windows PowerShell to shut down my computer. The command, Stop-Computer, is easy-to-use as shown here.

Stop-Computer

I can also use this to shut down multiple computers from across the network because the –ComputerName parameter accepts an array of computer names. The following command illustrates this technique. This is also a good command to use the –WhatIf parameter with.

PS C:\> Stop-Computer -ComputerName dc1,dc3 -WhatIf

What if: Performing operation "Stop-Computer" on Target " (dc1)".

What if: Performing operation "Stop-Computer" on Target " (dc3)".

PS C:\>

When you are certain you want to perform the action, use the Up arrow and remove the –WhatIf parameter. This command is shown here.

Stop-Computer -ComputerName dc1,dc3

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.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I'll add a twist to #2) Find a Date

    Find Out What Day Of The Week A Date Is

    If you want to know what day of the week your parent's were born on, you can enter

    (Get-Date 9/15/1936).DayOfWeek

  • @Michael Simmons that is a great tip. Plus it is easily discoverable when one pipes get-date | get-member I also love your way of creating an "arbritary" date: get-date 9/15/36 ... is way cool. thanks for sharing.

  • Hi Ed, this is my snip to kill stray explorer processes except the largest one, i.e. my desktop.   Sometimes a program opens a folder (via explorer.exe) but after closing the window the process is left running in memory just like iexplore. The largest one is usually my shell so I threw in a -Skip 1 to avoid refreshing my desktop.

    Get-Process explorer | Sort-Object -descending CPU | Select-Object -Skip 1 ID | ForEach-Object -Process {Stop-Process -Id $_.ID}