0
0
PowerShellscripting~20 mins

Writing files (Set-Content, Out-File) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell File Writer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output file content after running this script?
Consider this PowerShell script that writes text to a file using Set-Content. What will be the content of 'output.txt' after running it?
PowerShell
Set-Content -Path output.txt -Value "Hello, World!"
AThe file 'output.txt' contains the text: Hello, World!
BThe file 'output.txt' is empty
CThe file 'output.txt' contains the text: Set-Content -Path output.txt -Value "Hello, World!"
DThe file 'output.txt' contains the text: "Hello, World!" with quotes
Attempts:
2 left
💡 Hint
Set-Content writes the exact string given to the file without adding quotes.
💻 Command Output
intermediate
2:00remaining
What will be the content of 'log.txt' after this script runs?
This script appends text to a file using Out-File. What will 'log.txt' contain after running both commands?
PowerShell
Out-File -FilePath log.txt -InputObject "Start Log" -Encoding utf8
Out-File -FilePath log.txt -InputObject "End Log" -Encoding utf8 -Append
AOnly "End Log" in 'log.txt', replacing previous content
B"Start Log" on the first line and "End Log" on the second line in 'log.txt'
C"Start LogEnd Log" on a single line in 'log.txt'
DThe file 'log.txt' will be empty
Attempts:
2 left
💡 Hint
Out-File overwrites by default unless -Append is used.
📝 Syntax
advanced
2:00remaining
Which option correctly writes multiple lines to a file using Set-Content?
You want to write these three lines to 'notes.txt': Line1, Line2, Line3. Which command does this correctly?
ASet-Content notes.txt -Value "Line1", "Line2", "Line3"
BSet-Content -Path notes.txt -Value "Line1\nLine2\nLine3"
CSet-Content -Path notes.txt -Value @("Line1", "Line2", "Line3")
DSet-Content -Path notes.txt -Value "Line1"; "Line2"; "Line3"
Attempts:
2 left
💡 Hint
Set-Content accepts an array of strings to write multiple lines.
🔧 Debug
advanced
2:00remaining
Why does this Out-File command fail to append text?
This script is supposed to append "New Entry" to 'data.txt' but overwrites it instead. Why?
PowerShell
Out-File -FilePath data.txt -InputObject "New Entry"
ABecause -InputObject is not a valid parameter
BBecause Out-File cannot write strings to files
CBecause the file path is incorrect
DBecause the -Append parameter is missing, so it overwrites the file
Attempts:
2 left
💡 Hint
Check the parameters for appending with Out-File.
🚀 Application
expert
3:00remaining
How to write a UTF8 BOM encoded file with Set-Content?
You want to write the text "Data Export" to 'export.txt' with UTF8 encoding including BOM using Set-Content. Which command does this correctly?
ASet-Content -Path export.txt -Value "Data Export" -Encoding utf8bom
BSet-Content -Path export.txt -Value "Data Export" -Encoding utf8
CSet-Content -Path export.txt -Value "Data Export" -Encoding ascii
DSet-Content -Path export.txt -Value "Data Export"
Attempts:
2 left
💡 Hint
UTF8 BOM encoding requires the special encoding name 'utf8bom'.