Bird
Raised Fist0
PowerShellscripting~20 mins

Get-ChildItem for listing in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Get-ChildItem Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Get-ChildItem command?
You run this command in a folder containing three files: file1.txt, file2.log, and file3.txt.

Get-ChildItem -Filter *.txt

What will be listed?
PowerShell
Get-ChildItem -Filter *.txt
ALists file2.log only
BNo files are listed
CLists file1.txt and file3.txt only
DLists all files: file1.txt, file2.log, file3.txt
Attempts:
2 left
💡 Hint
Think about what the filter *.txt means.
💻 Command Output
intermediate
2:00remaining
What does this command output?
You run this command in a folder with subfolders and files:

Get-ChildItem -Recurse -Directory

What will it list?
PowerShell
Get-ChildItem -Recurse -Directory
ALists only top-level folders
BLists only files recursively
CLists all files and folders recursively
DLists only folders recursively
Attempts:
2 left
💡 Hint
Look at the -Directory switch.
📝 Syntax
advanced
2:00remaining
Which command correctly lists all hidden files in the current folder?
Choose the correct PowerShell command to list hidden files only.
AGet-ChildItem -Attributes Hidden
BGet-ChildItem -Attributes +Hidden
CGet-ChildItem -Hidden
DGet-ChildItem -Filter Hidden
Attempts:
2 left
💡 Hint
Check how to filter by attributes in Get-ChildItem.
🔧 Debug
advanced
2:00remaining
Why does this command fail to list files larger than 1MB?
You try to list files larger than 1MB with:

Get-ChildItem | Where-Object { $_.Length -gt 1MB }

But it returns no files. Why?
PowerShell
Get-ChildItem | Where-Object { $_.Length -gt 1MB }
A1MB is not a valid size unit in PowerShell expressions
BGet-ChildItem does not return Length property
CWhere-Object syntax is incorrect
DFiles larger than 1MB do not exist
Attempts:
2 left
💡 Hint
Check how to specify file size in bytes.
🚀 Application
expert
2:00remaining
How to list only files modified in the last 7 days?
You want to list files changed in the last 7 days using Get-ChildItem and Where-Object. Which command does this correctly?
AGet-ChildItem | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-7) }
BGet-ChildItem | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-7) }
CGet-ChildItem | Where-Object { $_.LastAccessTime -gt (Get-Date).AddDays(7) }
DGet-ChildItem | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(7) }
Attempts:
2 left
💡 Hint
Think about comparing LastWriteTime to 7 days ago.

Practice

(1/5)
1. What does the PowerShell command Get-ChildItem do by default?
easy
A. Lists all files and folders in the current directory
B. Deletes all files in the current directory
C. Creates a new folder in the current directory
D. Moves files to another directory

Solution

  1. Step 1: Understand the command purpose

    Get-ChildItem is used to list contents of a directory, including files and folders.
  2. Step 2: Check default behavior

    By default, it lists all files and folders in the current directory without deleting, creating, or moving anything.
  3. Final Answer:

    Lists all files and folders in the current directory -> Option A
  4. Quick Check:

    Get-ChildItem lists files/folders = A [OK]
Hint: Remember: Get-ChildItem lists, it does not delete or move [OK]
Common Mistakes:
  • Thinking it deletes files
  • Confusing it with file creation commands
  • Assuming it moves files automatically
2. Which of the following is the correct syntax to list only files (not folders) in the current directory using Get-ChildItem?
easy
A. Get-ChildItem -File
B. Get-ChildItem -Directory
C. Get-ChildItem -Folder
D. Get-ChildItem -ListFiles

Solution

  1. Step 1: Identify the parameter for files only

    The -File parameter filters output to show only files.
  2. Step 2: Verify other options

    -Directory lists only folders, -Folder and -ListFiles are invalid parameters.
  3. Final Answer:

    Get-ChildItem -File -> Option A
  4. Quick Check:

    -File lists files only = B [OK]
Hint: Use -File to list files, -Directory for folders [OK]
Common Mistakes:
  • Using -Directory to list files
  • Using invalid parameters like -Folder
  • Confusing parameter names
3. What will be the output of this command if the current directory contains two files: file1.txt, file2.log and one folder Docs?
Get-ChildItem -File
medium
A. Lists only the folder Docs
B. Lists file1.txt, file2.log, and Docs
C. Lists file1.txt and file2.log only
D. No output

Solution

  1. Step 1: Understand the -File parameter effect

    The -File parameter filters output to show only files, excluding folders.
  2. Step 2: Apply to given directory contents

    Given files file1.txt and file2.log will be listed; folder Docs will be excluded.
  3. Final Answer:

    Lists file1.txt and file2.log only -> Option C
  4. Quick Check:

    -File excludes folders = C [OK]
Hint: Remember -File shows files only, no folders [OK]
Common Mistakes:
  • Expecting folders to appear with -File
  • Confusing files and folders in output
  • Assuming -File lists everything
4. You run this command but get an error:
Get-ChildItem -Recurse -File -Folder
What is the problem?
medium
A. Get-ChildItem does not support -File parameter
B. The -Recurse parameter is invalid
C. The command is missing a path argument
D. You cannot use both -File and -Folder together

Solution

  1. Step 1: Check parameter compatibility

    -Folder is not a valid parameter; the correct parameter for folders is -Directory. Also, -File and -Directory cannot be used together because they filter mutually exclusive items.
  2. Step 2: Identify the error cause

    Using -File and -Folder together causes a parameter conflict error.
  3. Final Answer:

    You cannot use both -File and -Folder together -> Option D
  4. Quick Check:

    -File and -Directory are exclusive = A [OK]
Hint: Use either -File or -Directory, not both together [OK]
Common Mistakes:
  • Using -Folder instead of -Directory
  • Trying to combine -File and -Directory
  • Assuming -Recurse causes the error
5. You want to list all files with extension .txt in the current directory and all its subdirectories. Which command should you use?
hard
A. Get-ChildItem -Filter *.txt
B. Get-ChildItem -Recurse -File -Filter *.txt
C. Get-ChildItem -Recurse -Directory -Filter *.txt
D. Get-ChildItem -File -Filter *.txt

Solution

  1. Step 1: Include subdirectories with -Recurse

    To search inside all subfolders, use the -Recurse parameter.
  2. Step 2: Filter only files with .txt extension

    Use -File to list files only and -Filter *.txt to select files ending with .txt.
  3. Final Answer:

    Get-ChildItem -Recurse -File -Filter *.txt -> Option B
  4. Quick Check:

    Use -Recurse, -File, and -Filter for .txt files = D [OK]
Hint: Combine -Recurse, -File, and -Filter for specific files [OK]
Common Mistakes:
  • Omitting -Recurse to search subfolders
  • Using -Directory instead of -File
  • Not using -Filter for file extension
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