0
0
PowerShellscripting~10 mins

Writing files (Set-Content, Out-File) in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A'Hello World'
BHello World
C"Hello World"
DHelloWorld
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string value.
Using a variable name without defining it.
2fill in blank
medium

Complete 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'
A-NoClobber
B-Append
C-Force
D-Overwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using -Overwrite which replaces the file content.
Forgetting the dash '-' before the parameter.
3fill in blank
hard

Fix 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'
A$text
Btext
C'$text'
D$Text
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable name as a string instead of its value.
Forgetting the $ sign before the variable.
4fill in blank
hard

Fill 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'
A-Encoding UTF8
B-Append
C-NoClobber
D-Force
Attempts:
3 left
💡 Hint
Common Mistakes
Using -Append which adds instead of overwriting.
Using -NoClobber which prevents overwriting.
5fill in blank
hard

Fill 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'
AToUpper
B-Append
C-NoClobber
D-Force
Attempts:
3 left
💡 Hint
Common Mistakes
Using -NoClobber which prevents overwriting but does not append.
Forgetting the parentheses after ToUpper.