Skip to content

Instantly share code, notes, and snippets.

@NotNahid
Last active August 11, 2025 07:02
Show Gist options
  • Select an option

  • Save NotNahid/3705faee30a267a61db6f105c7f73166 to your computer and use it in GitHub Desktop.

Select an option

Save NotNahid/3705faee30a267a61db6f105c7f73166 to your computer and use it in GitHub Desktop.
param(
[switch]$Help,
[string]$Name = "User"
)
function Show-Help {
Write-Host @"
MyTool Usage:
-Help Show this help info
-Name <string> Specify your name for greeting
Menu Options:
1. Say Hello
2. Show Date and Time
3. Fetch Example API Data
4. Read from a file
5. Write to a file
6. Show log file
7. Open Website
8. Exit
"@ -ForegroundColor Yellow
}
if ($Help) {
Show-Help
exit
}
function Write-Log {
param($message)
$logFile = "$env:USERPROFILE\mytool.log"
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$entry = "$timestamp - $message"
Add-Content -Path $logFile -Value $entry
}
function Say-Hello {
Write-Host "Hello, $Name!" -ForegroundColor Green
Write-Log "Said hello to $Name"
}
function Show-Date {
$date = Get-Date
Write-Host "Current date and time: $date" -ForegroundColor Cyan
Write-Log "Displayed current date and time"
}
function Fetch-API {
$url = "https://api.agify.io/?name=$Name"
try {
$response = Invoke-RestMethod -Uri $url -ErrorAction Stop
Write-Host "API Guess: For the name '$($response.name)', the predicted age is $($response.age)" -ForegroundColor Magenta
Write-Log "Fetched API data: $response"
} catch {
Write-Host "Error fetching API: $_" -ForegroundColor Red
Write-Log "API fetch error: $_"
}
}
function Read-File {
$path = Read-Host "Enter path of file to read"
if (Test-Path $path -PathType Leaf) {
$content = Get-Content $path
Write-Host ("Contents of " + $path + ":") -ForegroundColor Yellow
$content | ForEach-Object { Write-Host $_ }
Write-Log "Read file: $path"
} else {
Write-Host "File does not exist or is not a file." -ForegroundColor Red
Write-Log "Failed to read file (not found or folder): $path"
}
}
function Write-File {
$path = Read-Host "Enter path of file to write"
$text = Read-Host "Enter text to write"
try {
$text | Out-File -FilePath $path -Encoding UTF8
Write-Host "Written to $path" -ForegroundColor Green
Write-Log "Wrote to file: $path"
} catch {
Write-Host "Error writing file: $_" -ForegroundColor Red
Write-Log "File write error: $_"
}
}
function Show-Log {
$logFile = "$env:USERPROFILE\mytool.log"
if (Test-Path $logFile) {
Write-Host "Log contents:" -ForegroundColor Yellow
Get-Content $logFile | ForEach-Object { Write-Host $_ }
} else {
Write-Host "Log file does not exist." -ForegroundColor Red
}
}
function Open-Website {
$url = "https://pustokkit.carrd.co/"
try {
Start-Process $url
Write-Host "Opened website: $url" -ForegroundColor Green
Write-Log "Opened website: $url"
} catch {
Write-Host "Failed to open website: $_" -ForegroundColor Red
Write-Log "Failed to open website: $_"
}
}
function Show-Menu {
Clear-Host
Write-Host "=== MyTool Interactive Menu ===" -ForegroundColor Cyan
Write-Host "1. Say Hello"
Write-Host "2. Show Date and Time"
Write-Host "3. Fetch Example API Data"
Write-Host "4. Read from a file"
Write-Host "5. Write to a file"
Write-Host "6. Show log file"
Write-Host "7. Open Website"
Write-Host "8. Exit"
}
while ($true) {
Show-Menu
$choice = Read-Host "Select an option"
switch ($choice) {
'1' { Say-Hello }
'2' { Show-Date }
'3' { Fetch-API }
'4' { Read-File }
'5' { Write-File }
'6' { Show-Log }
'7' { Open-Website }
'8' { Write-Host "Goodbye!" -ForegroundColor Cyan; break }
default { Write-Host "Invalid choice, try again." -ForegroundColor Red }
}
Write-Host "`nPress any key to continue..." -ForegroundColor DarkGray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment