Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Management with Get-Process and Stop-Process
📖 Scenario: You are managing your computer's running programs. Sometimes, a program uses too much memory or is not responding. You want to find these programs and stop them safely.
🎯 Goal: You will write a PowerShell script that lists running processes, sets a memory usage limit, finds processes using more memory than this limit, and stops those processes.
📋 What You'll Learn
Create a variable with a list of running processes using Get-Process
Create a variable called memoryLimitMB with a value of 100
Use a foreach loop to find processes using more than memoryLimitMB megabytes of memory
Stop those processes using Stop-Process
Print the names of stopped processes
💡 Why This Matters
🌍 Real World
System administrators often need to manage processes that use too much memory or cause problems. This script helps automate that task.
💼 Career
Knowing how to list, filter, and stop processes is useful for IT support, system administration, and automation roles.
Progress0 / 4 steps
1
Get the list of running processes
Create a variable called processes and set it to the list of all running processes using Get-Process.
PowerShell
Hint
Use Get-Process to get all running processes and assign it to processes.
2
Set the memory usage limit
Create a variable called memoryLimitMB and set it to 100 to represent 100 megabytes.
PowerShell
Hint
Just assign the number 100 to memoryLimitMB.
3
Find and stop processes using too much memory
Use a foreach loop with variable proc to go through processes. Inside the loop, check if proc.WorkingSet / 1MB is greater than memoryLimitMB. If yes, stop the process using Stop-Process -Id $proc.Id and print the process name with Write-Output.
PowerShell
Hint
Use foreach ($proc in $processes) to loop. Check memory with $proc.WorkingSet / 1MB. Use Stop-Process -Id $proc.Id to stop.
4
Run the script and see stopped processes
Run the script to see the names of processes stopped because they used more than 100 MB memory.
PowerShell
Hint
Run the script. If any process uses more than 100 MB, you will see lines like Stopped process: processname.
Practice
(1/5)
1. What does the PowerShell command Get-Process do?
easy
A. Lists all running processes on the computer
B. Stops a running process immediately
C. Starts a new process
D. Deletes a file from the system
Solution
Step 1: Understand the purpose of Get-Process
The command Get-Process is used to display information about processes currently running on the computer.
Step 2: Compare options with command function
Only Lists all running processes on the computer correctly describes listing running processes. Other options describe different actions not related to Get-Process.
Final Answer:
Lists all running processes on the computer -> Option A
Quick Check:
Get-Process lists processes = A [OK]
Hint: Get-Process always shows running programs, not stopping them [OK]
Common Mistakes:
Confusing Get-Process with Stop-Process
Thinking Get-Process starts or deletes processes
Assuming it modifies processes instead of listing
2. Which of the following is the correct syntax to stop a process named 'notepad' in PowerShell?
easy
A. Stop-Process notepad -Force
B. Get-Process -Stop notepad
C. Kill-Process -Name notepad
D. Stop-Process -Name notepad
Solution
Step 1: Identify correct cmdlet and parameter
The cmdlet to stop a process is Stop-Process. The parameter to specify process by name is -Name.
Step 2: Validate syntax correctness
Stop-Process -Name notepad uses correct cmdlet and parameter: Stop-Process -Name notepad. Get-Process -Stop notepad uses wrong cmdlet and parameter. Stop-Process notepad -Force misses the parameter name before 'notepad'. Kill-Process -Name notepad uses a non-existent cmdlet.
Final Answer:
Stop-Process -Name notepad -> Option D
Quick Check:
Stop-Process with -Name is correct syntax = C [OK]
Hint: Use Stop-Process with -Name to stop by process name [OK]
Common Mistakes:
Omitting the -Name parameter
Using incorrect cmdlet names like Kill-Process
Placing process name without parameter name
3. What will be the output of this PowerShell command?
A. Lists all running PowerShell processes without stopping them
B. Stops the PowerShell process and outputs the stopped process details
C. Throws an error because Stop-Process cannot be piped
D. Starts a new PowerShell process
Solution
Step 1: Understand the pipeline usage
The command gets the process named 'powershell' and pipes it to Stop-Process. The -PassThru parameter makes Stop-Process output the stopped process object.
Step 2: Predict command behavior
The process will be stopped, and its details will be shown as output. No error occurs because piping is supported.
Final Answer:
Stops the PowerShell process and outputs the stopped process details -> Option B
Quick Check:
Get-Process piped to Stop-Process with -PassThru stops and outputs = B [OK]
Hint: Stop-Process supports pipeline input and -PassThru outputs stopped process [OK]
D. Get-Process chrome | Stop-Process -MemoryUsage 100MB
Solution
Step 1: Understand memory property and filtering
The WorkingSet property shows memory usage in bytes. 100 MB equals 100,000,000 bytes approximately.
Step 2: Filter processes by memory and stop them
Get-Process -Name chrome | Where-Object { $_.WorkingSet -gt 100000000 } | Stop-Process correctly filters chrome processes with memory usage greater than 100,000,000 bytes and pipes them to Stop-Process. Get-Process -Name chrome | Where-Object { $_.WorkingSet -gt 100MB } | Stop-Process uses '100MB' which is invalid syntax. Options C and D use non-existent parameters.