Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to write the string 'Hello World' to a file named output.txt using Set-Content.
PowerShell
Set-Content -Path 'output.txt' -Value [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string value.
Using a variable name without defining it.
✗ Incorrect
The value to write must be a string, so it needs quotes. Single quotes are fine in PowerShell.
2fill in blank
mediumComplete the code to append the text 'New line' to the file log.txt using Out-File.
PowerShell
'New line' | Out-File -FilePath 'log.txt' [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -Overwrite which replaces the file content.
Forgetting the dash '-' before the parameter.
✗ Incorrect
The -Append parameter adds text to the end of the file instead of replacing it.
3fill in blank
hardFix the error in the code to write the content of variable $text to file data.txt using Set-Content.
PowerShell
Set-Content -Path 'data.txt' -Value [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable name as a string instead of its value.
Forgetting the $ sign before the variable.
✗ Incorrect
Variables in PowerShell start with $ and are case-insensitive, but best to match case exactly. $text is correct to pass the variable's content.
4fill in blank
hardFill both blanks to write 'Log entry' to file log.txt and overwrite existing content.
PowerShell
'Log entry' | Out-File -FilePath 'log.txt' [1] [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -Append which adds instead of overwriting.
Using -NoClobber which prevents overwriting.
✗ Incorrect
Use -Encoding UTF8 to specify encoding and -Force to overwrite the file if needed.
5fill in blank
hardFill both blanks to write the uppercase version of $message to file output.txt, appending to existing content.
PowerShell
$message.[1]() | Out-File -FilePath 'output.txt' [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -NoClobber which prevents overwriting but does not append.
Forgetting the parentheses after ToUpper.
✗ Incorrect
Use ToUpper() to convert text to uppercase and -Append to add to the file without overwriting.