Create Text File Using Windows PowerShell

Rohan Timalsina Feb 06, 2022
  1. Use New-Item to Create a New Text File Through Windows PowerShell
  2. Use ni Command to Create a New Text File Through Windows PowerShell
  3. Use fsutil to Create a New Text File Through Windows PowerShell
  4. Use the echo Command to Create a New Text File Through Windows PowerShell
  5. Use Text Editor to Create a New Text File Through Windows PowerShell
Create Text File Using Windows PowerShell

PowerShell can perform different standard operations on files and directories in the system. File management is one of them, and it is an important feature available for PowerShell users.

You can easily create, copy, move, remove, and rename files from the command line. This tutorial teaches to create a new text file through Windows PowerShell.

Use New-Item to Create a New Text File Through Windows PowerShell

The New-Item cmdlet creates a new file in the file system. The following command creates a testfile1.txt in C:\New\.

New-Item -Path "C:\New\" -Name "testfile1.txt" -ItemType File

Output:

    Directory: C:\New


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         1/28/2022  11:14 PM              0 testfile1.txt

It creates an empty text file. You can use the -Value parameter to put some content in a new file.

New-Item -Path "C:\New\" -Name "testfile2.txt" -ItemType File -Value "Hello World"

Output:

    Directory: C:\New


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         1/28/2022  11:14 PM             11 testfile2.txt

The Get-Content cmdlet is used to get the content of a file in the specified path.

Get-Content -Path C:\New\testfile2.txt

Output:

Hello World

Use ni Command to Create a New Text File Through Windows PowerShell

The ni is a built-in alias for the New-Item cmdlet. You can also use this command for creating a new file through Windows PowerShell.

ni -Path "C:\New\" -Name "testfile3.txt" -ItemType File

Output:

    Directory: C:\New


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         1/28/2022  11:23 PM              0 testfile3.txt

Use fsutil to Create a New Text File Through Windows PowerShell

The fsutil is a powerful command that can perform different tasks, including creating new files from the command line. It allows you to create a new file with a specific size.

For example, the following command creates a new file named testfile4.txt in the current directory with a size of 10000 bytes, i.e., 10KB.

fsutil file createnew testfile4.txt 10000

Output:

File C:\Users\rhntm\testfile4.txt is created

Use the echo Command to Create a New Text File Through Windows PowerShell

The echo command is a built-in alias for PowerShell’s Write-Output cmdlet. It is similar to other shells that use echo.

Both commands write the output to the pipeline and redirect the output to a new file. It will create a new file with the same output as its content.

The following command creates a new file, testfile5.txt, with specified contents in the current directory.

echo "PowerShell Tutorial" > testfile5.txt

Check the content of the new file.

Get-Content testfile5.txt

Output:

PowerShell Tutorial

Use Text Editor to Create a New Text File Through Windows PowerShell

You can use different text editors in PowerShell to create a new file, such as nano, notepad, vim, emacs, etc.

This command will prompt users to create a new file from the notepad.

notepad testfile6.txt

Similarly, you can install other text editors and create a new file with some content.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell File