0
0
PowerShellscripting~20 mins

Reading file content (Get-Content) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell File Reader 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 PowerShell command?
Given a file named example.txt containing the lines:

Hello
World

What will be the output of this command?

Get-Content example.txt
PowerShell
Get-Content example.txt
AError: File not found
BHello World
CSystem.Object[]
DHello`nWorld
Attempts:
2 left
💡 Hint
Get-Content reads each line as a separate string and outputs them line by line.
💻 Command Output
intermediate
2:00remaining
What does this command output when reading a file with empty lines?
Assume data.txt contains:

Line1



Line3

What is the output of:

Get-Content data.txt
PowerShell
Get-Content data.txt
ALine1`n`nLine3
BLine1`nLine3
CLine1 Line3
DError: Empty lines not allowed
Attempts:
2 left
💡 Hint
Get-Content preserves empty lines as empty strings in the output.
📝 Syntax
advanced
2:00remaining
Which command correctly reads the first 3 lines of a file?
You want to read only the first 3 lines of log.txt. Which command does this correctly?
AGet-Content log.txt -First 3
BGet-Content log.txt | Select-Object -First 3
CGet-Content log.txt -TotalCount 3
DGet-Content log.txt | Where-Object { $_ -le 3 }
Attempts:
2 left
💡 Hint
Look for the parameter that limits the number of lines read directly.
🔧 Debug
advanced
2:00remaining
Why does this command fail with an error?
You run:

Get-Content -Path

and get an error. Why?
PowerShell
Get-Content -Path
ABecause the -Path parameter requires a value specifying the file path.
BBecause Get-Content does not have a -Path parameter.
CBecause the file is empty.
DBecause the command needs -TotalCount parameter.
Attempts:
2 left
💡 Hint
Check if all required parameters have values.
🚀 Application
expert
3:00remaining
How to read a file and output only lines containing 'error' (case-insensitive)?
You want to read server.log and output only lines that contain the word 'error', ignoring case. Which command achieves this?
AGet-Content server.log | Select-String 'error' -CaseSensitive
BGet-Content server.log | Select-String 'error' -SimpleMatch
CGet-Content server.log | Where-Object { $_ -imatch 'error' }
DGet-Content server.log | Where-Object { $_ -match 'error' }
Attempts:
2 left
💡 Hint
Look for the operator that matches text ignoring case.