Bird
Raised Fist0
PowerShellscripting~20 mins

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

Choose your learning style10 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'.

Practice

(1/5)
1. What does the PowerShell command Set-Content do when used on a file?
easy
A. It writes new content to the file, replacing any existing content.
B. It appends new content to the end of the file without removing existing content.
C. It deletes the file from the system.
D. It reads the content of the file and displays it on the screen.

Solution

  1. Step 1: Understand Set-Content behavior

    Set-Content writes data to a file and replaces any existing content by default.
  2. Step 2: Compare with other commands

    Unlike Out-File with -Append, Set-Content does not append but overwrites.
  3. Final Answer:

    It writes new content to the file, replacing any existing content. -> Option A
  4. Quick Check:

    Set-Content replaces file content = D [OK]
Hint: Set-Content replaces file content, Out-File can append [OK]
Common Mistakes:
  • Confusing Set-Content with appending behavior
  • Thinking Set-Content reads files
  • Assuming it deletes files
2. Which of the following is the correct syntax to append text to a file using Out-File?
easy
A. Out-File -FilePath "log.txt" -InputObject "New entry" -Append
B. Out-File -FilePath "log.txt" -InputObject "New entry"
C. Set-Content -Path "log.txt" -Value "New entry" -Append
D. Set-Content -FilePath "log.txt" -InputObject "New entry"

Solution

  1. Step 1: Identify append syntax for Out-File

    Out-File uses the -Append parameter to add content without overwriting.
  2. Step 2: Check other options

    Set-Content does not support -Append; Out-File -FilePath "log.txt" -InputObject "New entry" -Append correctly uses Out-File with -Append.
  3. Final Answer:

    Out-File -FilePath "log.txt" -InputObject "New entry" -Append -> Option A
  4. Quick Check:

    Out-File append needs -Append flag = A [OK]
Hint: Use -Append with Out-File to add content, not Set-Content [OK]
Common Mistakes:
  • Using -Append with Set-Content (unsupported)
  • Omitting -Append when appending with Out-File
  • Mixing parameter names like -Value and -InputObject
3. What will be the content of the file output.txt after running this script?
"Line1" | Set-Content output.txt
"Line2" | Out-File output.txt -Append
"Line3" | Set-Content output.txt
medium
A. Line1\nLine2\nLine3
B. Line3
C. Line1\nLine2
D. Line1

Solution

  1. Step 1: Analyze first command

    Set-Content writes "Line1" to output.txt, replacing any existing content.
  2. Step 2: Analyze second command

    Out-File with -Append adds "Line2" after "Line1" in output.txt.
  3. Step 3: Analyze third command

    Set-Content overwrites the entire file with "Line3", removing previous lines.
  4. Final Answer:

    Line3 -> Option B
  5. Quick Check:

    Last Set-Content overwrites file = B [OK]
Hint: Last Set-Content overwrites file, ignoring previous appends [OK]
Common Mistakes:
  • Assuming appends persist after Set-Content
  • Confusing Out-File and Set-Content effects
  • Thinking all lines accumulate
4. You want to append the text "Error found" to a log file using Set-Content, but your script overwrites the file instead. What is the mistake?
medium
A. You need to use the -Force parameter with Set-Content to append.
B. You forgot to use the -Append parameter with Set-Content.
C. Set-Content does not support appending; use Out-File with -Append instead.
D. You should use Get-Content before Set-Content to append.

Solution

  1. Step 1: Understand Set-Content capabilities

    Set-Content replaces file content and does not have an append option.
  2. Step 2: Identify correct command for appending

    Out-File supports appending with the -Append parameter, which is the proper choice.
  3. Final Answer:

    Set-Content does not support appending; use Out-File with -Append instead. -> Option C
  4. Quick Check:

    Set-Content cannot append = C [OK]
Hint: Set-Content overwrites; use Out-File -Append to add text [OK]
Common Mistakes:
  • Trying to use -Append with Set-Content
  • Assuming -Force enables appending
  • Using Get-Content unnecessarily
5. You have a script that generates multiple lines of output stored in a variable $results (an array of strings). You want to save all lines to a file named "report.txt" without overwriting existing content. Which approach correctly appends all lines?
hard
A. Write-Output $results > "report.txt"
B. $results | Out-File -FilePath "report.txt"
C. $results | Set-Content -Path "report.txt"
D. Add-Content -Path "report.txt" -Value $results

Solution

  1. Step 1: Understand appending multiple lines

    Add-Content appends content to a file and accepts arrays to write multiple lines.
  2. Step 2: Compare other options

    Set-Content overwrites; Out-File without -Append overwrites; Write-Output with > overwrites.
  3. Final Answer:

    Add-Content -Path "report.txt" -Value $results -> Option D
  4. Quick Check:

    Add-Content appends arrays correctly = A [OK]
Hint: Use Add-Content to append arrays of lines easily [OK]
Common Mistakes:
  • Using Set-Content which overwrites
  • Using > redirection which overwrites
  • Not using -Append with Out-File
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