Bird
Raised Fist0
PowerShellscripting~20 mins

Test-Path for existence checks in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Test-Path Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this Test-Path command?
Given the following PowerShell command, what will be the output if the file C:\temp\file.txt exists?
PowerShell
Test-Path -Path 'C:\temp\file.txt'
ATrue
BFalse
CThrows an error
DReturns the file size
Attempts:
2 left
💡 Hint
Test-Path returns True if the path exists, otherwise False.
💻 Command Output
intermediate
1:30remaining
What does this command output if the directory does not exist?
What will be the output of this command if the directory C:\logs does NOT exist?
PowerShell
Test-Path -Path 'C:\logs' -PathType Container
ATrue
BThrows an error
CFalse
DReturns an empty string
Attempts:
2 left
💡 Hint
Test-Path returns False if the directory does not exist.
📝 Syntax
advanced
2:00remaining
Which option correctly checks if a file exists and is a leaf (file)?
Select the correct PowerShell command to check if C:\data\report.csv exists and is a file (not a directory).
ATest-Path 'C:\data\report.csv' -Type Leaf
BTest-Path -Path 'C:\data\report.csv' -PathType Container
CTest-Path -PathType Leaf 'C:\data\report.csv'
DTest-Path -Path 'C:\data\report.csv' -PathType Leaf
Attempts:
2 left
💡 Hint
The -PathType parameter must be followed by Leaf or Container to specify file or directory.
🚀 Application
advanced
2:00remaining
How to check multiple paths existence in one command?
You want to check if both C:\temp\file1.txt and C:\temp\file2.txt exist. Which command returns True only if both exist?
ATest-Path 'C:\temp\file1.txt', 'C:\temp\file2.txt' | Where-Object { $_ -eq $false }
B(Test-Path 'C:\temp\file1.txt') -and (Test-Path 'C:\temp\file2.txt')
CTest-Path -Path 'C:\temp\file1.txt' -and 'C:\temp\file2.txt'
DTest-Path 'C:\temp\file1.txt' -and Test-Path 'C:\temp\file2.txt'
Attempts:
2 left
💡 Hint
Use logical operators to combine multiple Test-Path results.
🔧 Debug
expert
2:30remaining
Why does this Test-Path command always return False?
Consider this command: Test-Path -Path 'C:/temp/file.txt'. Why might it always return False even if the file exists?
PowerShell
Test-Path -Path 'C:/temp/file.txt'
ABecause Test-Path requires backslashes \ not forward slashes / in Windows paths
BBecause Test-Path cannot check files, only directories
CBecause the file extension is missing
DBecause the path string is not quoted
Attempts:
2 left
💡 Hint
Windows paths usually use backslashes. Forward slashes may cause path not found.

Practice

(1/5)
1. What does the PowerShell command Test-Path 'C:\Users\Public' check?
easy
A. If the path 'C:\Users\Public' is a network location
B. If the file 'Public' exists inside 'C:\Users'
C. If the current user has permission to access 'C:\Users\Public'
D. If the folder 'C:\Users\Public' exists on the system

Solution

  1. Step 1: Understand the purpose of Test-Path

    Test-Path checks if a file or folder path exists on the system.
  2. Step 2: Analyze the given path

    The command checks if the folder 'C:\Users\Public' exists, not permissions or network status.
  3. Final Answer:

    If the folder 'C:\Users\Public' exists on the system -> Option D
  4. Quick Check:

    Test-Path checks existence = A [OK]
Hint: Test-Path returns True if path exists, else False [OK]
Common Mistakes:
  • Confusing existence check with permission check
  • Thinking it checks file contents
  • Assuming it checks network paths only
2. Which of the following is the correct syntax to check if a file named log.txt exists in the current directory using Test-Path?
easy
A. Test-Path log.txt
B. Test-Path -File log.txt
C. Test-Path -f ./log.txt
D. Test-Path -Exists log.txt

Solution

  1. Step 1: Recall Test-Path basic syntax

    Test-Path takes a path string as its argument to check existence.
  2. Step 2: Evaluate each option

    Test-Path log.txt uses correct syntax. A uses invalid -f parameter (Unix-style). B and D use invalid parameters -File and -Exists.
  3. Final Answer:

    Test-Path log.txt -> Option A
  4. Quick Check:

    Correct syntax is Test-Path + path [OK]
Hint: Use Test-Path followed by path string without extra flags [OK]
Common Mistakes:
  • Adding unsupported parameters like -File or -Exists
  • Using Unix-style parameters like -f
  • Confusing Test-Path with Get-Item syntax
3. What will be the output of this PowerShell code?
$path = 'C:\Windows\System32'
if (Test-Path $path) { 'Exists' } else { 'Not Found' }
medium
A. Not Found
B. Exists
C. Error: Invalid path
D. No output

Solution

  1. Step 1: Understand the path being checked

    'C:\Windows\System32' is a standard Windows system folder that exists on most Windows machines.
  2. Step 2: Evaluate the if condition

    Test-Path returns True, so the if block outputs 'Exists'.
  3. Final Answer:

    Exists -> Option B
  4. Quick Check:

    Existing folder returns True = 'Exists' [OK]
Hint: Test-Path returns True for existing paths, triggering 'Exists' output [OK]
Common Mistakes:
  • Assuming the path might not exist on Windows
  • Confusing output with error messages
  • Ignoring the if-else structure
4. You wrote this script to check if a folder exists:
if (Test-Path -Path 'D:\Data') {
  Write-Host 'Folder found'
} else {
  Write-Host 'Folder missing'
}

But it always prints 'Folder missing' even when the folder exists. What is the likely problem?
medium
A. PowerShell requires double backslashes in paths
B. Test-Path does not accept the -Path parameter
C. The folder path is incorrect or misspelled
D. Write-Host cannot print strings

Solution

  1. Step 1: Check Test-Path syntax

    Test-Path accepts the -Path parameter correctly, so syntax is fine.
  2. Step 2: Consider common causes for false result

    If it always prints 'Folder missing', the folder path is likely wrong or misspelled, causing Test-Path to return False.
  3. Final Answer:

    The folder path is incorrect or misspelled -> Option C
  4. Quick Check:

    Wrong path causes Test-Path False = D [OK]
Hint: Check folder path spelling if Test-Path always returns False [OK]
Common Mistakes:
  • Thinking -Path is invalid parameter
  • Assuming Write-Host can't print
  • Believing backslashes need doubling in single quotes
5. You want to write a script that checks multiple file paths stored in an array and outputs only those that exist. Which PowerShell snippet correctly uses Test-Path to do this?
hard
A.
$files = @('file1.txt', 'file2.txt', 'file3.txt')
foreach ($f in $files) {
  if (Test-Path $f) { Write-Output $f }
}
B.
$files = @('file1.txt', 'file2.txt', 'file3.txt')
foreach ($f in $files) {
  if (Test-Path -Exists $f) { Write-Output $f }
}
C.
$files = @('file1.txt', 'file2.txt', 'file3.txt')
foreach ($f in $files) {
  if (Test-Path $files) { Write-Output $f }
}
D.
$files = @('file1.txt', 'file2.txt', 'file3.txt')
foreach ($f in $files) {
  if (Test-Path $f) { Write-Host 'File exists' }
}

Solution

  1. Step 1: Understand the goal

    The script should check each file path and output the file name if it exists.
  2. Step 2: Evaluate each option

    $files = @('file1.txt', 'file2.txt', 'file3.txt')
    foreach ($f in $files) {
      if (Test-Path $f) { Write-Output $f }
    }
    correctly loops over each file, checks existence with Test-Path, and outputs the file name.
    $files = @('file1.txt', 'file2.txt', 'file3.txt')
    foreach ($f in $files) {
      if (Test-Path -Exists $f) { Write-Output $f }
    }
    uses invalid -Exists parameter.
    $files = @('file1.txt', 'file2.txt', 'file3.txt')
    foreach ($f in $files) {
      if (Test-Path $files) { Write-Output $f }
    }
    incorrectly tests the whole array each time.
    $files = @('file1.txt', 'file2.txt', 'file3.txt')
    foreach ($f in $files) {
      if (Test-Path $f) { Write-Host 'File exists' }
    }
    outputs a generic message, not the file name.
  3. Final Answer:

    Option A code snippet -> Option A
  4. Quick Check:

    Loop + Test-Path per file + output file name = A [OK]
Hint: Loop files, Test-Path each, output file name if True [OK]
Common Mistakes:
  • Using invalid parameters like -Exists
  • Testing whole array instead of single file
  • Outputting generic messages instead of file names
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard