0
0
PowerShellscripting~5 mins

New-Item for creation in PowerShell

Choose your learning style9 modes available
Introduction

You use New-Item to create new files or folders easily with a simple command.

You want to create a new text file to save notes.
You need to make a new folder to organize your documents.
You want to create a new empty file before adding content.
You are automating a task that requires new folders or files to be made.
You want to quickly create a file or folder without opening a file explorer.
Syntax
PowerShell
New-Item -Path <string> -ItemType <string> [-Name <string>] [-Value <string>] [-Force]

-Path is where you want to create the item.

-ItemType can be File or Directory (folder).

Examples
This creates a new file called notes.txt in the Documents folder.
PowerShell
New-Item -Path C:\Users\User\Documents -Name "notes.txt" -ItemType File
This creates a new folder called Projects in the Documents folder.
PowerShell
New-Item -Path C:\Users\User\Documents -Name "Projects" -ItemType Directory
This creates a file log.txt with initial text Log start.
PowerShell
New-Item -Path C:\Users\User\Documents\log.txt -ItemType File -Value "Log start"
Sample Program

This script creates a new file called example.txt in the current folder with the text Hello, world!. Then it reads and shows the content of the file.

PowerShell
New-Item -Path . -Name "example.txt" -ItemType File -Value "Hello, world!"
Get-Content -Path "example.txt"
OutputSuccess
Important Notes

If the file or folder already exists, New-Item will give an error unless you use -Force to overwrite.

You can create nested folders by specifying the full path and using -Force.

Summary

New-Item creates files or folders quickly.

Use -ItemType File for files and -ItemType Directory for folders.

You can add initial content to files with -Value.