0
0
PowerShellscripting~20 mins

New-Item for creation in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell New-Item Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this New-Item command?
You run this PowerShell command to create a new file:
New-Item -Path . -Name "testfile.txt" -ItemType File -Value "Hello" -Force

What will be the output?
PowerShell
New-Item -Path . -Name "testfile.txt" -ItemType File -Value "Hello" -Force
A
Directory: C:\YourCurrentDirectory

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2024-06-01     12:00           5 testfile.txt
BNew-Item : Cannot create a file when that file already exists.
CNew-Item : The term 'New-Item' is not recognized as the name of a cmdlet.
DNew-Item : Access to the path 'testfile.txt' is denied.
Attempts:
2 left
💡 Hint
Think about what -Force does and what output New-Item shows when successful.
📝 Syntax
intermediate
1:30remaining
Which New-Item command correctly creates a new directory named 'Logs'?
Choose the correct PowerShell command to create a directory named 'Logs' in the current folder.
ANew-Item -Path . -Name Logs -Item Directory
BNew-Item -Path . -Name Logs -ItemType File
CNew-Item -Path . -Name Logs -Type Directory
DNew-Item -Path . -Name Logs -ItemType Directory
Attempts:
2 left
💡 Hint
The parameter to specify the type of item is '-ItemType'.
🔧 Debug
advanced
2:00remaining
Why does this New-Item command fail with an error?
You run:
New-Item -Path C:\Temp -Name "data.txt" -ItemType File -Value "Sample"

But get an error:
New-Item : Access to the path 'C:\Temp\data.txt' is denied.

What is the most likely cause?
AThe file 'data.txt' already exists and -Force was not used.
BYou do not have permission to write to the C:\Temp folder.
CThe -Value parameter cannot be used with -ItemType File.
DThe path C:\Temp does not exist.
Attempts:
2 left
💡 Hint
Think about file system permissions and common causes of 'Access denied'.
🚀 Application
advanced
2:30remaining
How to create multiple empty text files named file1.txt to file5.txt using New-Item?
You want to create 5 empty text files named file1.txt, file2.txt, ..., file5.txt in the current directory using a single PowerShell command or script. Which option correctly does this?
A1..5 | ForEach-Object { New-Item -Name "file$_.txt" -ItemType File -Value '' }
BNew-Item -Path . -Name "file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt" -ItemType File
CFor ($i=1; $i -le 5; $i++) { New-Item -Name "file$i.txt" -ItemType Directory }
DNew-Item -Name "file{1..5}.txt" -ItemType File
Attempts:
2 left
💡 Hint
Think about looping and how to create files with dynamic names.
🧠 Conceptual
expert
1:30remaining
What is the effect of the -Force parameter in New-Item?
Consider the command:
New-Item -Path . -Name "example.txt" -ItemType File -Value "Data" -Force

What does the -Force parameter do in this context?
AIt creates a hidden file instead of a normal file.
BIt forces the command to run with administrator privileges.
CIt overwrites the file if it already exists without prompting.
DIt creates the file only if it does not exist, otherwise throws an error.
Attempts:
2 left
💡 Hint
Think about what happens if the file already exists and you want to replace it.