Here's a comprehensive PowerShell script that implements a todo list manager according to your specifications:
<#
.SYNOPSIS
A command-line todo list manager with CRUD functionality.
.DESCRIPTION
Manages tasks with properties including description, due date, priority, and status.
Tasks are stored in a JSON file and can be created, read, updated, and deleted.
.EXAMPLE
.\TodoManager.ps1 -Add "Complete project report" -DueDate "2024-01-30" -Priority "High"
Adds a new high-priority task with a due date.
.EXAMPLE
.\TodoManager.ps1 -List --pending
Lists all pending tasks.
#>
param (
[Parameter(ParameterSetName='Add')]
[string]$Add,
[Parameter(ParameterSetName='Add')]
[string]$DueDate,
[Parameter(ParameterSetName='Add')]
[ValidateSet('Low', 'Medium', 'High')]
[string]$Priority = 'Medium',
[Parameter(ParameterSetName='List')]
[switch]$List,
[Parameter(ParameterSetName='List')]
[ValidateSet('--all', '--pending', '--completed')]
[string]$Filter = '--all',
[Parameter(ParameterSetName='Update')]
[int]$Update,
[Parameter(ParameterSetName='Update')]
[string]$Description,
[Parameter(ParameterSetName='Update')]
[string]$NewDueDate,
[Parameter(ParameterSetName='Update')]
[ValidateSet('Low', 'Medium', 'High')]
[string]$NewPriority,
[Parameter(ParameterSetName='Update')]
[ValidateSet('Pending', 'Completed')]
[string]$Status,
[Parameter(ParameterSetName='Delete')]
[int]$Delete
)
# Constants
$TASKS_FILE = "tarefas.json"
$PRIORITY_COLORS = @{
'Low' = 'Green'
'Medium' = 'Yellow'
'High' = 'Red'
}
$PRIORITY_EMOJIS = @{
'Low' = '⭐'
'Medium' = '⭐⭐'
'High' = '⭐⭐⭐'
}
$STATUS_EMOJIS = @{
'Pending' = '🕒'
'Completed' = '✅'
}
# Helper Functions
function Initialize-TasksFile {
if (-not (Test-Path $TASKS_FILE)) {
@() | ConvertTo-Json | Set-Content $TASKS_FILE
}
}
function Get-Tasks {
try {
$content = Get-Content $TASKS_FILE -Raw
if ([string]::IsNullOrEmpty($content)) {
return @()
}
return $content | ConvertFrom-Json
}
catch {
Write-Error "Error reading tasks file: $_"
return @()
}
}
function Save-Tasks {
param ($tasks)
try {
$tasks | ConvertTo-Json | Set-Content $TASKS_FILE
}
catch {
Write-Error "Error saving tasks: $_"
}
}
function Format-TaskOutput {
param ($task)
$priorityEmoji = $PRIORITY_EMOJIS[$task.Priority]
$statusEmoji = $STATUS_EMOJIS[$task.Status]
$dueDateStr = if ($task.DueDate) { $task.DueDate } else { "No due date" }
Write-Host -NoNewline "[$($task.Id)] " -ForegroundColor Cyan
Write-Host -NoNewline "$priorityEmoji " -ForegroundColor $PRIORITY_COLORS[$task.Priority]
Write-Host -NoNewline "$($task.Description) "
Write-Host -NoNewline "(Due: $dueDateStr) "
Write-Host "$statusEmoji"
}
# Main Functions
function Add-NewTask {
param ($description, $dueDate, $priority)
try {
if ([string]::IsNullOrEmpty($description)) {
throw "Description cannot be empty"
}
if ($dueDate -and -not ([datetime]::TryParse($dueDate, [ref]$null))) {
throw "Invalid date format"
}
$tasks = Get-Tasks
$newId = if ($tasks.Count -eq 0) { 1 } else { ($tasks.Id | Measure-Object -Maximum).Maximum + 1 }
$newTask = @{
Id = $newId
Description = $description
CreationDate = Get-Date -Format "yyyy-MM-dd"
DueDate = $dueDate
Priority = $priority
Status = "Pending"
}
$tasks += $newTask
Save-Tasks $tasks
Write-Host "Task added successfully!" -ForegroundColor Green
}
catch {
Write-Error "Error adding task: $_"
}
}
function List-Tasks {
param ($filter)
$tasks = Get-Tasks
switch ($filter) {
'--pending' { $tasks = $tasks | Where-Object { $_.Status -eq 'Pending' } }
'--completed' { $tasks = $tasks | Where-Object { $_.Status -eq 'Completed' } }
}
if ($tasks.Count -eq 0) {
Write-Host "No tasks found." -ForegroundColor Yellow
return
}
foreach ($task in $tasks) {
Format-TaskOutput $task
}
}
function Update-Task {
param ($id, $description, $dueDate, $priority, $status)
try {
$tasks = Get-Tasks
$task = $tasks | Where-Object { $_.Id -eq $id }
if (-not $task) {
throw "Task with ID $id not found"
}
if ($description) { $task.Description = $description }
if ($dueDate) {
if ([datetime]::TryParse($dueDate, [ref]$null)) {
$task.DueDate = $dueDate
}
else {
throw "Invalid date format"
}
}
if ($priority) { $task.Priority = $priority }
if ($status) { $task.Status = $status }
Save-Tasks $tasks
Write-Host "Task updated successfully!" -ForegroundColor Green
}
catch {
Write-Error "Error updating task: $_"
}
}
function Remove-Task {
param ($id)
try {
$tasks = Get-Tasks
$updatedTasks = $tasks | Where-Object { $_.Id -ne $id }
if ($tasks.Count -eq $updatedTasks.Count) {
throw "Task with ID $id not found"
}
Save-Tasks $updatedTasks
Write-Host "Task deleted successfully!" -ForegroundColor Green
}
catch {
Write-Error "Error deleting task: $_"
}
}
# Main Script
Initialize-TasksFile
switch ($PSCmdlet.ParameterSetName) {
'Add' { Add-NewTask -description $Add -dueDate $DueDate -priority $Priority }
'List' { List-Tasks -filter $Filter }
'Update' { Update-Task -id $Update -description $Description -dueDate $NewDueDate -priority $NewPriority -status $Status }
'Delete' { Remove-Task -id $Delete }
default { Write-Host "Please specify a valid command. Use Get-Help for usage information." -ForegroundColor Yellow }
}
This script provides:
- Full CRUD functionality for managing tasks
- JSON file storage with all required task properties
- Input validation and error handling
- Color-coded output with emojis for priority and status
- Comment-based help documentation
- Parameter validation using PowerShell parameter sets
To use the script, save it as TodoManager.ps1 and run it with various commands:
# Add a new task
.\TodoManager.ps1 -Add "Complete report" -DueDate "2024-01-30" -Priority "High"
# List all tasks
.\TodoManager.ps1 -List --all
# List pending tasks
.\TodoManager.ps1 -List --pending
# Update a task
.\TodoManager.ps1 -Update 1 -Description "Updated description" -Status "Completed"
# Delete a task
.\TodoManager.ps1 -Delete 1
The script includes error handling for:
- File operations
- Invalid date formats
- Missing or invalid task IDs
- Invalid command syntax
The output is formatted with color-coding and emojis for better visibility, and tasks are stored in a JSON file for persistence between sessions.