📖 Scenario: You are managing files and folders on your computer. Sometimes you need to check if a file or folder exists before doing something with it. This helps avoid errors and keeps your scripts safe.
🎯 Goal: Build a simple PowerShell script that checks if a specific file and a specific folder exist using Test-Path. Then, display the results clearly.
📋 What You'll Learn
Create variables holding exact file and folder paths
Use Test-Path to check existence of the file and folder
Store the results in variables
Print the results with clear messages
💡 Why This Matters
🌍 Real World
Checking if files or folders exist is a common task in automation scripts to avoid errors when reading or writing data.
💼 Career
System administrators and automation engineers often use Test-Path to verify resources before running commands or deploying software.
Progress0 / 4 steps
1
Create variables for file and folder paths
Create two variables: $filePath with the value "C:\Users\Public\Documents\example.txt" and $folderPath with the value "C:\Users\Public\Documents\Reports".
PowerShell
Hint
Use double quotes for the paths and double backslashes \\ to separate folders.
2
Add variables to store existence check results
Create two variables: $fileExists and $folderExists. Use Test-Path with $filePath to set $fileExists, and with $folderPath to set $folderExists.
PowerShell
Hint
Use Test-Path followed by the variable holding the path to get a True or False result.
3
Write conditional messages based on existence
Use if statements to print "File exists." if $fileExists is true, otherwise print "File does not exist.". Do the same for $folderExists with messages "Folder exists." and "Folder does not exist.".
PowerShell
Hint
Use if ($variable) { ... } else { ... } to check True or False and print messages.
4
Display the existence check results
Run the script to display the messages about the file and folder existence.
PowerShell
Hint
Run the script in PowerShell. It will print messages about the file and folder existence.
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
Step 1: Understand the purpose of Test-Path
Test-Path checks if a file or folder path exists on the system.
Step 2: Analyze the given path
The command checks if the folder 'C:\Users\Public' exists, not permissions or network status.
Final Answer:
If the folder 'C:\Users\Public' exists on the system -> Option D
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
Step 1: Recall Test-Path basic syntax
Test-Path takes a path string as its argument to check existence.
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.
Final Answer:
Test-Path log.txt -> Option A
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?
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
Step 1: Understand the goal
The script should check each file path and output the file name if it exists.
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.
Final Answer:
Option A code snippet -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.