Bird
0
0

You wrote this script to send an email alert if disk space is low:

medium📝 Troubleshoot Q14 of 15
PowerShell - Automation Patterns
You wrote this script to send an email alert if disk space is low:
$freeSpace = (Get-PSDrive C).Free
if ($freeSpace -lt "100MB") {
  Send-MailMessage -From 'alert@example.com' -To 'admin@example.com' -Subject 'Low Disk Space' -Body 'Disk space below 100MB' -SmtpServer 'smtp.example.com'
}

But no email is sent even when disk space is low. What is the likely problem?
AThe if condition should use -gt instead of -lt
BGet-PSDrive does not return Free space
CSend-MailMessage requires -Port parameter
D100MB is not a valid size unit in PowerShell comparison
Step-by-Step Solution
Solution:
  1. Step 1: Check the size comparison

    PowerShell does not recognize '100MB' as a valid number; it must be converted to bytes.
  2. Step 2: Understand Get-PSDrive output

    Get-PSDrive returns Free space in bytes as a number, so comparing to '100MB' string fails silently.
  3. Final Answer:

    100MB is not a valid size unit in PowerShell comparison -> Option D
  4. Quick Check:

    Size units must be numeric bytes [OK]
Quick Trick: Use numeric bytes, not '100MB' string in comparisons [OK]
Common Mistakes:
  • Assuming Get-PSDrive Free is not available
  • Thinking Send-MailMessage needs -Port always
  • Confusing less than and greater than operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes