How to create a new file in Powershell?

new-item keyword creates a new file in the filesystem. The type of item new item creates depends on where the item is being created. For example, new item can also create registry keys and entries if used in the registry.

Type the following command in to create a new file in the current path:

ni {filename.extension}

 

The above is the alias for the following command…

new-item {filename.extension}

 

Example

ni fun.txt

 

Parameters can also be used with new item.

The following creates a file in the current path with some content:

ni -n fun.txt -p . -it f -v 'this is some silly text'

 

In the above example we have added the -name, -path and -itemtype parameters with their default values. Omitting these will generate the same result.

ni fun.txt -v 'this is some silly text'

 

If you want to overwrite an existing file use the -force or -f parameter:

ni fun.txt -v 'this is some silly text' -f

 

Create multiple files in the current directory:

ni -it f -p 'file.txt', 'fun.txt'

 

Create multiple files in the c:/ root and things directory

ni -it f -p 'c:\file.txt', 'c:\things\fun.txt'

 

Creating Directories

You can create a new directory instead of mkdir or md by using the new item command and setting the item type as directory.

ni fun -it d

 

Create a new directory in the c root:

ni -n fun -it d -p c:\

 

Create multiple directories in the current path:

ni -it d -p 'fun', 'silly'

 

Create multiple directories in the c:/ root and things directory

ni -it d -p 'c:\folder', 'c:\things\fun'

 

The following table shows the parameters and it’s alias

Parameter Alias Example Description
-name
-n or no parameter
ni fun.txt
Specify the new item’s name
-itemtype
-it
-it d Lossis -it dir Lossis -it directory
-it f Lossis -it file
Hais kom meej hom khoom los tsim. Tsim los ntawm default.
-quab yuam
-f
-f
Overwrite uas twb muaj lawm.
-Nqi
-v
-v 'Some file content...'
Sau ntawv qhia txog cov ntsiab lus uas sau rau hauv
-Kab
-p
-p c.:\
Txoj kev uas cov khoom tshiab yuav tsum tau tsim

Ib txhia commands uas yog pab tau nrog cov ni command:

ls – Get-ChildItem, teev cov ntaub ntawv thiab directories ntawm tus thawj coj tam sim no ua hauj lwm thawj coj. eg ls

gc – Get-Content, Saib cov txheej txheem ntawm ib daim ntawv eg. gc fun.txt

md – mkdir, thawj coj. md mynewdirectory

rm– Remove-Item, tshem tawm cov thawj coj los yog ntaub ntawv

 

Nom Microsoft Docs