📖 Scenario: You are organizing your project folder. You need to create a new folder and a new file inside it using PowerShell commands.
🎯 Goal: Learn how to use the New-Item cmdlet to create a folder and a file in PowerShell.
📋 What You'll Learn
Create a new folder named ProjectDocs
Create a new text file named README.txt inside the ProjectDocs folder
Use the New-Item cmdlet for both creations
Display the full path of the created file
💡 Why This Matters
🌍 Real World
Creating folders and files automatically is common when setting up projects or organizing data.
💼 Career
System administrators and automation engineers often use PowerShell scripts to prepare environments and manage files efficiently.
Progress0 / 4 steps
1
Create a new folder named ProjectDocs
Write a PowerShell command using New-Item to create a folder named ProjectDocs in the current directory.
PowerShell
Hint
Use -ItemType Directory to create a folder.
2
Set the folder path in a variable
Create a variable called $folderPath and set it to the path ./ProjectDocs.
PowerShell
Hint
Use $folderPath = './ProjectDocs' to store the folder path.
3
Create a new text file README.txt inside ProjectDocs
Use New-Item with -Path set to $folderPath and -Name set to README.txt to create a new file inside the folder.
PowerShell
Hint
Use -ItemType File to create a file.
4
Display the full path of the created README.txt file
Assign the result of the New-Item command creating README.txt to a variable called $newFile. Then print the full path of the file using Write-Output and $newFile.FullName.
PowerShell
Hint
Store the created file in $newFile and print $newFile.FullName.
Practice
(1/5)
1. What does the PowerShell command New-Item -Path 'C:\Test' -ItemType Directory do?
easy
A. Creates a new file named 'Test' in the C drive.
B. Creates a new folder named 'Test' in the C drive.
C. Deletes the folder named 'Test' in the C drive.
D. Lists the contents of the 'Test' folder in the C drive.
Solution
Step 1: Understand the command parameters
The command uses New-Item with -ItemType Directory, which means it will create a folder (directory).
Step 2: Analyze the path parameter
The -Path 'C:\Test' specifies the location and name of the new folder to create on the C drive.
Final Answer:
Creates a new folder named 'Test' in the C drive. -> Option B
Quick Check:
New-Item with -ItemType Directory creates folders [OK]
Hint: Use -ItemType Directory to create folders quickly [OK]
Common Mistakes:
Confusing -ItemType File with Directory
Assuming it creates files by default
Misunderstanding the -Path parameter
2. Which of the following is the correct syntax to create a new empty file named notes.txt in PowerShell?
easy
A. New-Item -Path 'notes.txt' -ItemType File
B. New-Item -Path 'notes.txt' -ItemType Directory
C. New-Item -Path 'notes.txt' -Type File
D. New-Item -File 'notes.txt'
Solution
Step 1: Identify the correct parameter for file creation
The correct parameter to specify the type is -ItemType File to create a file.
Step 2: Check the syntax correctness
New-Item -Path 'notes.txt' -ItemType File uses New-Item -Path 'notes.txt' -ItemType File, which is the correct syntax to create a file named 'notes.txt'.
Final Answer:
New-Item -Path 'notes.txt' -ItemType File -> Option A
Quick Check:
Use -ItemType File to create files [OK]
Hint: Use -ItemType File to create files, not Directory [OK]
Common Mistakes:
Using -ItemType Directory for files
Using incorrect parameter names like -Type or -File
Omitting -ItemType parameter
3. What will be the output of this PowerShell command?
But you get an error saying the path does not exist. What is the likely fix?
medium
A. Run PowerShell as administrator
B. Change -ItemType File to -ItemType Directory
C. Remove the -Value parameter
D. Create the folder 'C:\Data' before running the command
Solution
Step 1: Understand the error cause
The error indicates the folder path 'C:\Data' does not exist, so the file cannot be created inside it.
Step 2: Fix the path issue
Creating the folder 'C:\Data' first ensures the path exists for the file creation.
Final Answer:
Create the folder 'C:\Data' before running the command -> Option D
Quick Check:
Parent folders must exist before creating files [OK]
Hint: Ensure parent folders exist before creating files [OK]
Common Mistakes:
Assuming -ItemType Directory fixes path errors
Ignoring missing parent folders
Thinking -Value causes the error
5. You want to create a folder named Projects inside C:\Work and inside it create a file README.md with the text Project Start. Which sequence of commands achieves this?