powershell

Powershell : Killing all processes of name

Now I am just starting with PowerShell, so I will likely come up with some ridiculously simple examples.

One of thing I really like about PowerShell is the ability to pipe things from one CmdLet  to another.

Imagine you want to get all instances of the the running process “notepad” and kill them.

This is easily achieved using the following code

GetProcess "notepad" | StopProcess

 

Like  I say this is ridiculously simple, and hardy worthy of a blog post at all, but I aim to build a set of common tasks posts, and this will just form one of those.

So until next time

13 thoughts on “Powershell : Killing all processes of name

  1. I have been wondering about Powershell as well for sometime, but have not yet found use for it, as I’m a developer and not an administrator. However, I have noted this in the back of my mind, in case I ever get in a situation where one has a lot processes running or a self spawning process that won’t go away.

    1. I am a dev too, thing is we use continuous integration/deployment, which means lots of scripts. Right now we use BAT files and NANT files. But PowerShell is way more powerful hands down. Its “wicked awesome dude”, as my son would say

    2. Ever buy a tool for some specific job around the house, and a year later find you used that tool in ways you didn’t imagine when you bought it? That’s how PowerShell is.

      I’m a dev too, and ramping up on PowerShell. Have used cmd/batch files for decades.

      I’m finding that PowerShell commandlets for various development tasks are much better in versatility and expressiveness than DOS based commands. For some development work, like SharePoint development, PowerShell is extremely useful.

    3. I’m a Dev, and use PowerShell for Dev purposes all the time. Even simple things are super useful.

      PS> gci -inc bin,obj -rec | rm -rec -force

      That wipes out all of the “bin” and “obj” directories in the current directory and every subdirectory. Super useful to run in your workspace directory to get to a “clean” state, especially when someone messes up and there’s something that a Clean or Rebuild inside the IDE doesn’t catch.

      I’ve written PowerShell scripts to find resource names in Resx files that aren’t referenced in code anywhere, something super useful to do before sending your Resx out to be translated by some company that charges you by the word.

      I’ve written PowerShell scripts to automate certain repetitive tasks, like creating new branches in source control.

      “I’m a developer and not an admistrator” is no reason to not learn and use PowerShell. 😉

      1. Thanks for that Bill. We use a shed load of NANT to automate things right now, but I am really digging powershell a lot too. Think I’m going to post your up (full credits obviously).

      2. I guess my environment causes me not to learn it then, being a Visual Studio with SourceSafe , these things are already cared for. We also have a tight development environment with only two developers and a systems architect. Don’t get me wrong, I understand it is powerful, but still couldn’t see any advantages specifically for us that isn’t covered in our tools. As have investigated it though, looking at examples and tutorials around the web – haven’t seen anything I would use to resolve an issue I can handle in another manner. The reason I was refferring to administrators is because most of the examples I saw was for administrative tasks.

  2. I prefer MSBuild over NANT, but both of those are for automating builds. The things I use PowerShell for have little or nothing to do with builds. They’re generally one-off or infrequent things that you do that would be repetitive, time consuming and even sheer drudgery to do by hand, but take you a second or two to type a command into PowerShell or a minute or two to create a script for. I use PowerShell nearly every day, and my favorite extension in Visual Studio is the “Package Manager” installed by NuGet (so, now a part of the IDE proper, I guess) which puts PowerShell inside the IDE. I don’t use it much, but one of the coolest things I’ve ever worked with is StudioShell (http://studioshell.codeplex.com/) which allows you to automate things inside of Visual Studio using PowerShell.

  3. I am getting the following error when attempting to run this on Windows 7:

    The term ‘Get-Process’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

    At line:1 char:12
    + Get-Process <<<< "iexplore" | Stop-Process
    + CategoryInfo : ObjectNotFound: (Get-Process:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

  4. Anyone getting “not recognized” after copypasting from this webpage: it’s because the code in the article uses the wrong dashes in the command name.

    Replace all the — with – and it’ll work for you.

Leave a comment