Bird
0
0

You want to write a PowerShell script that creates a folder named 'Logs' only on Windows and macOS, but skips Linux. Which code snippet correctly implements this platform-specific behavior?

hard📝 Application Q15 of 15
PowerShell - Cross-Platform PowerShell
You want to write a PowerShell script that creates a folder named 'Logs' only on Windows and macOS, but skips Linux. Which code snippet correctly implements this platform-specific behavior?
Aif ($IsWindows -or $IsMacOS) { New-Item -ItemType Directory -Path './Logs' }
Bif ($IsLinux) { New-Item -ItemType Directory -Path './Logs' }
Cif ($IsWindows -and $IsMacOS) { New-Item -ItemType Directory -Path './Logs' }
Dif ($IsUnix) { New-Item -ItemType Directory -Path './Logs' }
Step-by-Step Solution
Solution:
  1. Step 1: Understand platform conditions

    You want to create the folder only on Windows or macOS, so the condition should check if either $IsWindows or $IsMacOS is true.
  2. Step 2: Analyze each option

    if ($IsWindows -or $IsMacOS) { New-Item -ItemType Directory -Path './Logs' } uses -or to combine $IsWindows and $IsMacOS correctly. if ($IsLinux) { New-Item -ItemType Directory -Path './Logs' } creates folder only on Linux (wrong). if ($IsWindows -and $IsMacOS) { New-Item -ItemType Directory -Path './Logs' } uses -and which requires both true (impossible). if ($IsUnix) { New-Item -ItemType Directory -Path './Logs' } uses undefined $IsUnix which is falsey (wrong).
  3. Final Answer:

    if ($IsWindows -or $IsMacOS) { New-Item -ItemType Directory -Path './Logs' } -> Option A
  4. Quick Check:

    Use -or for Windows or Mac condition [OK]
Quick Trick: Use -or to combine platform checks for multiple OS [OK]
Common Mistakes:
  • Using -and instead of -or for multiple platforms
  • Creating folder on Linux by mistake
  • Using undefined variables like $IsUnix

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes