Set-Content writes "Line1" to output.txt, replacing any existing content.
Step 2: Analyze second command
Out-File with -Append adds "Line2" after "Line1" in output.txt.
Step 3: Analyze third command
Set-Content overwrites the entire file with "Line3", removing previous lines.
Final Answer:
Line3 -> Option B
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
Step 1: Understand Set-Content capabilities
Set-Content replaces file content and does not have an append option.
Step 2: Identify correct command for appending
Out-File supports appending with the -Append parameter, which is the proper choice.
Final Answer:
Set-Content does not support appending; use Out-File with -Append instead. -> Option C
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
Step 1: Understand appending multiple lines
Add-Content appends content to a file and accepts arrays to write multiple lines.
Step 2: Compare other options
Set-Content overwrites; Out-File without -Append overwrites; Write-Output with > overwrites.
Final Answer:
Add-Content -Path "report.txt" -Value $results -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.