Task Manager and Process Monitor

Description:

This PowerShell script creates a simple task manager and process monitor. It allows users to view running processes, sort them by various criteria, and perform basic actions like stopping a process. The script demonstrates working with processes, user input handling, and basic data manipulation in PowerShell.

Full Script:

# Task Manager and Process Monitor

# Function to display the menu
function Show-Menu {
    Clear-Host
    Write-Host "=== Task Manager and Process Monitor ===" -ForegroundColor Cyan
    Write-Host "1. View all running processes"
    Write-Host "2. View top 10 processes by CPU usage"
    Write-Host "3. View top 10 processes by Memory usage"
    Write-Host "4. Search for a process"
    Write-Host "5. Stop a process"
    Write-Host "6. Exit"
}

# Function to view all running processes
function View-AllProcesses {
    Get-Process | Format-Table Id, ProcessName, CPU, WorkingSet -AutoSize
}

# Function to view top 10 processes by CPU usage
function View-TopCPUProcesses {
    Get-Process | Sort-Object CPU -Descending | Select-Object -First 10 | 
        Format-Table Id, ProcessName, CPU -AutoSize
}

# Function to view top 10 processes by Memory usage
function View-TopMemoryProcesses {
    Get-Process | Sort-Object WorkingSet -Descending | Select-Object -First 10 | 
        Format-Table Id, ProcessName, @{Name="Memory (MB)"; Expression={[math]::Round($_.WorkingSet / 1MB, 2)}} -AutoSize
}

# Function to search for a process
function Search-Process {
    $searchTerm = Read-Host "Enter process name to search"
    Get-Process | Where-Object {$_.ProcessName -like "*$searchTerm*"} | 
        Format-Table Id, ProcessName, CPU, WorkingSet -AutoSize
}

# Function to stop a process
function Stop-UserProcess {
    $processId = Read-Host "Enter the Process ID to stop"
    try {
        Stop-Process -Id $processId -Force -ErrorAction Stop
        Write-Host "Process with ID $processId has been stopped." -ForegroundColor Green
    }
    catch {
        Write-Host "Error: Unable to stop process. $($_.Exception.Message)" -ForegroundColor Red
    }
}

# Main program loop
do {
    Show-Menu
    $choice = Read-Host "`nEnter your choice (1-6)"

    switch ($choice) {
        "1" { View-AllProcesses }
        "2" { View-TopCPUProcesses }
        "3" { View-TopMemoryProcesses }
        "4" { Search-Process }
        "5" { Stop-UserProcess }
        "6" { Write-Host "Exiting program..." -ForegroundColor Yellow; break }
        default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red }
    }

    if ($choice -ne "6") {
        Read-Host "`nPress Enter to continue..."
    }
} while ($choice -ne "6")

This script includes:

  1. A menu display function for user interaction
  2. Functions to view all processes, top CPU-consuming processes, and top memory-consuming processes
  3. A function to search for processes by name
  4. A function to stop a process by its ID
  5. Use of PowerShell cmdlets like Get-Process, Stop-Process, and Format-Table
  6. Sorting and filtering of process data
  7. Basic error handling when stopping a process
  8. Formatting of output for better readability

The script provides a simple interface for users to monitor and manage system processes, making it suitable for beginners to understand PowerShell scripting concepts, process management, and data manipulation. It also introduces concepts like error handling and custom formatting of output data.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *