0
0
PowerShellscripting~5 mins

Test-Path for existence checks in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the PowerShell cmdlet <code>Test-Path</code> do?

Test-Path checks if a file, folder, or other item exists at a specified path. It returns True if the item exists, and False if it does not.

Click to reveal answer
beginner
How do you check if a folder exists using Test-Path?

Use Test-Path with the folder path as a string. For example: Test-Path 'C:\Users\Public\Documents'. It returns True if the folder exists.

Click to reveal answer
beginner
What output does Test-Path return if the path does not exist?

Test-Path returns False if the specified path does not exist.

Click to reveal answer
intermediate
Can Test-Path check for the existence of registry keys?

Yes, Test-Path can check if registry keys exist by specifying the registry path, like HKLM:\Software\Microsoft.

Click to reveal answer
beginner
How can you use Test-Path in a script to perform an action only if a file exists?

You can use if (Test-Path 'path') { # action } to run code only when the file or folder exists.

Click to reveal answer
What does Test-Path 'C:\temp\file.txt' return if the file exists?
AFalse
BTrue
CError
DNull
Which PowerShell cmdlet checks if a path exists?
ATest-Path
BRemove-Item
CSet-Location
DGet-Item
What will Test-Path return if the path does not exist?
AReturns the path string
BTrue
CThrows an error
DFalse
Can Test-Path check for registry keys?
ANo, only files and folders
BOnly for files inside registry
CYes, by specifying the registry path
DOnly with extra modules
How do you use Test-Path in a script to run code only if a file exists?
Aif (Test-Path 'path') { # code }
BTest-Path 'path' | Remove-Item
CSet-Location 'path'
DGet-Content 'path'
Explain how Test-Path helps in checking if a file or folder exists before running a script action.
Think about avoiding errors by confirming the file or folder is there first.
You got /4 concepts.
    Describe how you would use Test-Path to check for a registry key's existence.
    Registry paths start with HKLM or HKCU.
    You got /3 concepts.