You can use the text file to save your notes and bookmarks for future reference. You can also edit it with a word processor, such as Microsoft Word or Google Docs. The only requirement is that you have access to the Command Prompt on Windows.
In Windows 10, the “how to create a file in cmd windows 10” is an easy way to create text files. The command line tool that can be used for this purpose is called ‘notepad’.
Right-click on your desktop or in File Explorer and choose New to create a file in Windows. Is this going to be the shortest post of all time? Nope. Because the GUI may not always function, you may create a file using the command prompt.
You’ll learn how to create a file on the command line in this lesson, including how to use the Windows command prompt and PowerShell!
Let’s get going!
Prerequisites
If you want to follow along with this lesson, make sure you have the following items handy:
- A Windows computer – This tutorial will utilize Windows 10, however Windows 7 and above will also work.
- All demonstrations in this lesson will utilize PowerShell v7.1.3.
Using the Windows Command Prompt to Create a File
The command prompt (cmd.exe) and PowerShell are the two command-line alternatives in Windows. Let’s start by creating a blank file using the Windows command prompt.
To begin, click the Start button, type cmd.exe, then press Enter to launch the Windows command prompt. You should now see the Windows command prompt, and you’re ready to begin.
How to Use the ECHO Command
Let’s get this course started by going through the echo command. The echo command is a command-line tool for displaying messages in a command prompt. If you can send anything to the command prompt in Windows, you can also save the output to a file!
In the Windows Command Prompt window, type the following:
1. Run the echo command as shown below. This command echoes the text provided (This is a sample text file) to the command console. But, since the command is using the redirection operator (>), this operator tells Windows to instead create a file with the text (sample.txt).
Run the dir command once you’ve finished creating the file to get a list of all the files in the current working directory.
echo This is a sample text file > sample.txt dir
Windows produces the sample.txt file in the current working directory, as seen below.
forwarding text to sample.txt output
2. Type type followed by the file name to validate the file includes the anticipated “This is a sample text file” content. The type command reads a file and displays the contents.
As you can see in the screenshot below, sample.txt has the required content.
type output displaying the contents of sample.txt
How to Use the COPY CON Command
If you don’t like the echo command, you may always use the copy command with the con parameter. The con option instructs the copy command to copy output to the console rather than files.
The con argument is a gadget rather than a command.
To make a file with copy con, follow these steps:
1. Type copy con and then a file name. This command will not create the file right away. A flashing cursor will appear in front of you.
2. When you see the flashing cursor, put some text into the file and hit Enter.
This is an example of what you might create.
Press Ctrl-Z and Enter to leave the interactive prompt when you’re finished. When you press Ctrl-Z, Windows will add an end-of-file marker and shut the open file handle.
As you can see below, pressing Ctrl-Z displays Z and Windows informs you that one file has been “copied.” This is the file you specified, and it will contain the content you inputted.
To create a file end marker, use Ctrl-Z.
How to Use the FSUTIL Command
Introducing fsutil, the next Windows command prompt tool! Windows includes a file system administration utility called Fsutil.
To create a new file using fsutil at the command prompt, type:
1. If the command prompt is still active, close it and reopen it as an administrator. The fsutil tool, unfortunately, needs it.
Administrating Command Prompt
2. Now, use the createnew parameter with the filename to create (sample.txt) and the size in bytes you’d want the file to be in the fsutil file function to create a new file (1000).
The program below will generate a 1KB file named sample.txt in the current working directory.
createnew sample.txt 1000 fsutil
3. Type dir again to see a list of all files in the current working directory, and you should notice a 1,000-byte file.
Make a file with a set size.
The fsutil software is ideal for producing fake files for network performance testing since it can produce blank files of a certain size.
Using PowerShell to Create a File
It’s time to create a file using PowerShell, a more current technique to utilizing a Windows command prompt. Let’s go through all of the numerous methods to create a file with PowerShell.
So, open your Windows PowerShell or PowerShell console and Let’s get going!
Using the Cmdlet New-Item
The New-Item cmdlet is one of the simplest methods to create a file in Windows using PowerShell. This command generates new objects (a file in this case).
The New-Item cmdlet may also create registry entries and folders in Windows.
Related:How to Check whether a File Exists Using PowerShell (Examples)
1. Run the New-Item cmdlet, giving the path to the file (Path), the file name (Name), the item type (ItemType), and the text to include in the file (Text) (Value).
The command below will create a file named sample.txt in the current working directory (.) with the text This is a text string inside.
. -Name “sample.txt” New-Item -Path -Value “This is a text string” -ItemType “file”
Using the PowerShell New-Item cmdlet to create a new file
Using the Directory argument for the ItemType option, you can also use New-Item to construct a directory.
2. Now, use the Get-Content cmdlet to instruct PowerShell to read and display the contents of the file you just created.
The Get-Content cmdlet in PowerShell displays the contents of a file.
Using the Cmdlets Set-Content and Add-Content
Next, we’ll look at the PowerShell cmdlets Set-Content and Add-Content. These two cmdlets can both create files, however Set-Content creates and overwrites a file, whilst Add-Content creates and adds to an existing file.
Run the Set-Content cmdlet with PowerShell still open, giving it the Path and Name of the file to create, as well as the text you want the file to contain (Value), as shown below.
The following command will create a file named sample.txt in the current working directory, with the text This is a text string inside.
Set-Content -Path.sample.txt -Value “This is a text string” Set-Content -Path.sample.txt
Simply replace Set-Content with Add-Content to make the identical file. The Add-Content command will create files and add text to existing ones.
Get-Content in PowerShell: Reading Text Files Like a Boss
Using the Cmdlet Out-File
Let’s finish this lesson by using the Out-File cmdlet to create a file on the command line using PowerShell.
The Out-File cmdlet is a handy command similar to the redirection operator (>) previously covered in the Windows Command Prompt section. The Out-File cmdlet accepts input from the PowerShell console and writes that output to a file of your choosing.
Type some text (“This is a test”) in PowerShell and feed it to the Out-File cmdlet, specifying the path of the file to generate (FilePath).
The command below will create a file named sample.txt in the current working directory that has the text This is a test.
“This is a test” | Out-File.sample.txt -FilePath
PowerShell produced a 16-byte file named sample.txt, as seen below.
Using the PowerShell Out-File cmdlet to create a file
Conclusion
This lesson has shown you six distinct methods to create files from the command line, using both Windows and PowerShell. By now, you should be familiar with almost every command-line method for creating text files.
Which approach is your favorite?
The “cmd create empty file” will allow you to create a text file on the Command Prompt in Windows. The command is relatively easy, and doesn’t require any advanced knowledge of the Command Prompt.
Related Tags
- create text file command line linux
- dos command to create a file
- create file command line linux
- create file command line mac
- touch command in windows