0
0
PowerShellscripting~10 mins

Test-Path for existence checks in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the file 'example.txt' exists.

PowerShell
Test-Path [1]
Drag options to blanks, or click blank then click option'
ARemove-Item 'example.txt'
BGet-Item 'example.txt'
C'example.txt'
DNew-Item 'example.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using cmdlets like Get-Item instead of a path string.
Not putting the path in quotes.
2fill in blank
medium

Complete the code to check if the directory 'C:\Data' exists.

PowerShell
Test-Path [1]
Drag options to blanks, or click blank then click option'
A'C:\Data'
B'C:/Data'
CC:\Data
D'C:\\Data'
Attempts:
3 left
💡 Hint
Common Mistakes
Using forward slashes in Windows paths.
Not quoting the path string properly.
3fill in blank
hard

Fix the error in the code to check if 'log.txt' exists in the current directory.

PowerShell
if (Test-Path [1]) { Write-Output 'Exists' } else { Write-Output 'Missing' }
Drag options to blanks, or click blank then click option'
AGet-Item 'log.txt'
B'log.txt'
C"log.txt"
Dlog.txt
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the filename.
Using Get-Item instead of a path string.
4fill in blank
hard

Fill both blanks to create a hashtable of files with their existence status for 'file1.txt' and 'file2.txt'.

PowerShell
$files = @{ 'file1' = Test-Path [1]; 'file2' = Test-Path [2] }
Drag options to blanks, or click blank then click option'
A'file1.txt'
B'file2.txt'
C'file3.txt'
D'file4.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing file names or using wrong file names.
Not quoting the file names.
5fill in blank
hard

Fill all three blanks to create a filtered list of existing '.log' files from an array.

PowerShell
$logs = @('app.log', 'error.log', 'readme.txt') | Where-Object { Test-Path [1] } | ForEach-Object { $_.[2] } | Where-Object { $_.[3] -eq '.log' }
Drag options to blanks, or click blank then click option'
A'./' + $_
BSplit-Path
CExtension
DLength
Attempts:
3 left
💡 Hint
Common Mistakes
Not building the path correctly for Test-Path.
Using wrong properties to get the file extension.