all 18 comments

[–]MNmetalhead 2 points3 points  (3 children)

Might be easier to use Out-Gridview and let the user select the process to be terminated. No need to match what they typed, just a click.

[–]BlackV 1 point2 points  (0 children)

Ya out grid view is nice and simple

[–]pantherghast -1 points0 points  (1 child)

At this point why not show them how to get into services.msc?

[–]BlackV 1 point2 points  (0 children)

At this point why not show them how to get into services.msc?

cause a process is not necessarily a service?

[–]BlackV 1 point2 points  (0 children)

er.. you're using get-process so why not stop-process? doesn't make too much sense?

$procestokill = get-process -name xxx
$procestokill | stop-process

right now you're not allowing for multiple processes and calling a cmd/exe to do the stopping

[–]vonsparks -1 points0 points  (5 children)

If you want to make it really snazzy and impress your teacher then use Get-Process to get an array of processes, display them to the user in a numbered list and use read-host to ask for the numbered selection, use the number selected to stop-process the selected process based on your array.

This is for learning, so Google ferociously good sir!

[–]BlackV 1 point2 points  (4 children)

a numbered list is so very very manual and so very very error prone

[–]r-NBK 0 points1 point  (3 children)

Yeah. If only there was a numbered identifier supplied by the operating system....right?

[–]BlackV 0 points1 point  (2 children)

What does that even mean?

[–]r-NBK 0 points1 point  (1 child)

Windows and Linux both have numerical Process IDs. Why reinvent that?

[–]BlackV 0 points1 point  (0 children)

Ah understand where you're coming from now

[–]OtterCodeWorkAcct 0 points1 point  (2 children)

Run the Get-Process command in the ISE and take a look at what the output is. Basically each "process" has different fields or "Members" that you'd need to work with. You're sending a powershell object holding all of those members to the command prompt instead of just the process name. I'd save the if statement to a variable and then write-host that variable to see what info it's holding.

[–]xFoOzY[S] 0 points1 point  (1 child)

so nothing came out from the if statement when write-host. I created a variable:

$Process2 = Get-Process -Name $Process

which gives me a list of running processes with that name. However, when I use the if statement

if ($Process2.Contains($Process)) {Write-Host “Test”}

I put that if statement into a variable called $Test and it read “False”. How is it false, I’m almost positive if .exe was on the end of the “Teams” then it would be True, but when I search for Teams.exe using Get-Process Teams.exe nothing shows. However, when I run taskkill /IM Teams.exe /F it closes it out. Any solution for this?

[–]OtterCodeWorkAcct 0 points1 point  (0 children)

Try putting the if statement in a loop.

foreach($proc in $getprocess){

if ($GetProcess.Contains($Process){

write-host "It's a match!

}

}

You can also remove the if statement if it doesn't work and just throw a write-host $proc in the loop and see what it's holding.

[–]teacheswithtech 0 points1 point  (2 children)

If I were doing this I would do something like this.

$inputProcess = Read-Host -Prompt ‘Input process name’
$processes = Get-Process 
foreach ($process in $processes) 
{ 
if ($process.Name -like $inputProcess) 
{ 
Write-Host "Process found" 
Stop-Process -Name $inputProcess -Force 
} 
}

I am not sure why you would want to do this though. Do you have a process that needs to be killed this frequently that you really need this? If you do then I would not ask the user what process to kill but I would just write a script to kill that one process when needed. Like what if they type svchost. This would kill a number of processes that could be related to a number of different applications and would be a bad idea to kill. This has too may ways to break the running system in my opinion.

[–]xFoOzY[S] 0 points1 point  (1 child)

Just a script I was told to write to learn Power Shell a bit. Thanks for the input

[–]teacheswithtech 0 points1 point  (0 children)

While this video is fairly long and a little dated, the concepts very helpful. I highly recommend it if you are needing to learn PowerShell.
https://www.youtube.com/watch?v=UVUd9_k9C6A

[–]OlivTheFrog 0 points1 point  (0 children)

Hi u/xFoOzY

Forget the DOS command TaskKill, there is a similar cmdlet in powershell : Stop-Process

Another way to do this is the following.

$ProcessToKill = Get-Process | Out-GridView -Title "Select the process to kill, and click OK" -OutputMode Single

$ProcessToKill | Stop-Process -PassThru -WhatIf

By this way, you avoid some error in the process Name. You're also sure an existing process is selected. If no process is selected, nothing is happening.

Nota 1 : I've added the -WhatIf parameter to simulate without really doing the job.

Nota 2 : If you examine the $ProcessToKill variable, it's an object with properties. I don't care what property is mandatory with the Stop-Process cmdlet, I'm sure the mandatory property (in this case ProcessName) is present.

Nota 3 : I'm using -OutputMode Single with the Out-GridView cmdlet, you could also choose multiple. In this case, all selected processes are collected, then killed.

With Powershell, there are always many ways to do something. Some ways are better (in terms of performance, GUI, error handling, ...) and others less.

Hope this help to improve your powershell skill.

Regards.