How to create file in cmd
How to create file in cmd
Create file from command line
We can create files from command line in two ways. The first way is to use fsutil command and the other way is to use echo command. If you want to write any specific data in the file then use echo command. If you are not bothered about the data in the file but just want to create a file of some specific size then you can use fsutil command.
To create file using echo
To create file using fsutil
Limitations
Fsutil can be used only by administrators. For non-admin users it throws up below error.
Thank you for the quick and dirty method since windows seems to lack the “touch” command.
How can we use these commands to ‘touch’ a file. Could anyone provide the exact command that can work as linux ‘touch’ for any type of file?
Windows does not lack “touch”. It is echo. As the post describes? type: echo “” Blah.cpp
for Windows users, try
echo README.md
touch works on a UNIX system.
dude, this isnt working on our school’s computers, can you tell another method that works on windows 8 computers please?
lmao it works at school PC
Make sure that you are typing in the code the right way. It happened to me when I typed in “echo This is a sample text file sample.txt”.
If you want to create a file with NO text in it do this:
at the CLI type “Echo.”: Your output should look like this:
C:\Temp>echo.
To write to a file:
C:\Temp> Echo. >test.txt
This will create a file with single space in the file.
Utilizing PowerShell and Windows Command Prompt to Create File
Read more tutorials by Nick Rimmer!
Table of Contents
To create a file in Windows, right-click on your desktop or in File Explorer and click on New. Is this post going to be the shortest post ever? Nope. Because using the GUI sometimes won’t work, you can use the command prompt to create a file. In this tutorial, you will learn just about every way possible to use PowerShell and Windows command prompt to create file on the command line.
Let’s get started!
Table of Contents
Prerequisites
If you’d like to follow along with the steps in this tutorial, be sure you have the following ahead of time:
Using Windows Command Prompt to Create File
You have two command-line options in Windows; the command prompt (cmd.exe) or PowerShell. Let’s first cover how to use the Windows command prompt to create a file that is blank.
To get started, open the Windows command prompt by clicking on the Start button, typing cmd.exe, and hitting Enter. The Windows command prompt should come up, and you’re ready to go.
Using the ECHO Command
Let’s start this tutorial by first covering the echo command. The echo command is a command-line utility that allows you to display messages in a command prompt. In Windows, if you can send something to the command console, you can also redirect that output to a file!
In the Windows Command Prompt window:
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 ).
When you’ve created the file, run the dir command to list all files in the current working directory
You can see below that Windows creates the sample.txt file in the current working directory.
2. To confirm the file contains the expected “This is a sample text file” text, execute the type command followed by the file name. The type command reads the file and returns the text inside.
You can see below that sample.txt does, indeed, contain the expected text.
Using the COPY CON Command
If the echo command doesn’t fit your taste, you can always use the copy command using the con argument. Instead of copying files, the con argument tells the copy command to copy output to the console.
The con argument is actually a device, not a command.
To create a file using copy con :
1. Run copy con followed by a file name. This command won’t immediately create the file. You’ll be presented with a flashing cursor.
2. Once you see that flashing cursor type in some text you’d like to include in the file and press Enter.
When you’re done, press Ctrl-Z and press Enter to exit the interactive prompt. Pressing Ctrl-Z will also add an end-of-file marker to tell Windows to close the open file handle.
Using the FSUTIL Command
You can also use Windows command prompt to create file by introducing fsutil! Fsutil is a file system management tool built into Windows.
To use fsutil on the command prompt to create a new file:
1. Close the command prompt if you still have it open and open it up again, but this time as an administrator. Unfortunately, the fsutil utility requires it.
2. Now, invoke the file function of fsutil to create a new file using the createnew argument followed by the file name to create ( sample.txt ) and the size in bytes you’d like the file to be ( 1000 ).
The below command will create a file called sample.txt in the current working directory that’s 1KB in size.
3. Type dir again to list all files in the current working directory and you should see a 1,000-byte file now exists.
Since the fsutil utility can create blank files of a certain size, the utility is great for creating dummy files for network performance testing!
Creating a File with PowerShell
It’s now time to use a more modern approach of using a Windows command prompt to create file; with PowerShell! PowerShell has a few different ways to create a file so let’s cover them all.
So, open your Windows PowerShell or PowerShell console and let’s get started!
Using the New-Item Cmdlet
One of the easiest ways to create a file in Windows with PowerShell is to use the New-Item cmdlet This cmdlet creates new items (a file in this case).
The New-Item cmdlet can also create Windows registry keys or folders.
1. Run the New-Item cmdlet specifying the directory to create the file in ( Path ), the Name of the file, the type of item ( ItemType ), and finally, the text to include in the file ( Value ).
You can also use New-Item to create a directory by using the Directory argument for the ItemType parameter.
2. Now, take a look at the file just created using the Get-Content cmdlet to tell PowerShell to read the file and display its contents.
Using the Set-Content and Add-Content Cmdlets
Next, let’s check out the Set-Content and Add-Content PowerShell cmdlets. These two cmdlets can create files in a similar fashion but Set-Content creates and overwrites a file while Add-Content will create and append to an existing file.
With PowerShell still open, run the Set-Content cmdlet providing it the Path and name of the file to create with the text you’d like the file to contain ( Value ) as shown below.
The command below will create a file called sample.txt in the current working directory with the text This is a text string inside the file.
Using the Out-File Cmdlet
To round out how to create a file on the command prompt with PowerShell, let’s end this tutorial with the Out-File cmdlet.
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.
With PowerShell still open, type some text ( «This is a test» ) and pipe that text to the Out-File cmdlet providing the path to the file to create ( FilePath ).
The below command will create a file called sample.txt in the current working directory with the text This is a test inside.
You can see below that PowerShell created a 16-byte file called sample.txt.
Conclusion
In this tutorial you learned different ways to use PowerShell and Windows command prompt to create file. By now, you should know just about all the ways to create text files on the command line.
Which method do you prefer?
Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.
More from ATA Learning & Partners
Recommended Resources!
Recommended Resources for Training, Information Security, Automation, and more!
Get Paid to Write!
ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?
ATA Learning Guidebooks
ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!
Useful Commands to Manage Files and Folders through CMD in Windows 11/10
This article lists down various commands that you can use to manage files and folders through Command-Line in Windows 11/10. Although a lot of users prefer using a graphical user interface to manage files for a hassle-free experience, some also use the command-line interface to perform file management tasks. In any case, it is always better to know alternative solutions to execute a task.
In this guide, I will be creating a list of useful commands that you can use for file or folder management on your Windows 10 PC. To perform a specific task on files or folders, there is a dedicated command that you need to enter in CMD. Let’s check out these commands!
Commands to Manage Files and Folders through CMD
Here are the commands that you should know to manage files and folders using Command Prompt in Windows 11/10:
1] Create a File or Folder in CMD
To create a folder, type the folder name with the location where you want to create the folder. Here is the command:
To create a file of a specific size (in bytes), use the below command:
2] Delete Files or Folder in CMD
You can remove a folder using the below command:
In order to delete a file, the command is:
If you want to delete all files from the current folder, enter the command:
To delete files with a specific extension only, say png, use the command:
If you want to delete files with a particular string in their filename, e.g., xyz, you can use the below command:
3] Find Files in a Particular Folder
To find files inside a folder based on different parameters, you first need to navigate to the folder using the command:
Now, you can find files older than n days in a specific folder using the below command:
To find files larger than a specific size, use the command:
In the above command, 3741824 is the file size to search files greater than this size.
4] Rename all file extensions present in a folder at once
You can also batch rename file extensions in CMD. Suppose, you want to rename the file extension of all images to JPG, you can use the below command:
5] Get File Creation Time and Date
To check the creation time and date of a specific file, use the command:
6] Check for a string inside a file
To find all lines containing a particular string in a file, you can use the command:
For example, to display all lines with “twc” in a text file, you need to enter the command:
Do remember that the above command is case-sensitive.
To find sentences with any specified string, use a command like:
7] Check for all Hidden Files in a Folder
Use the below command to get a list of hidden files in a directory:
8] Compress a File in CMD
The command to compress a file in a folder is:
9] Hide/ Unhide a file through CMD
To hide a file, the command used is:
You can unhide the file again using the command:
10] Set/ Unset Read-Only attribute to a file
To make a file read-only, the command is:
If you want to remove the read-only attribute from a file, the command is:
11] Command to Rename a File/Folder
12] Read File Content in CMD
You can read text file content in CMD using the below command:
13] Open a File in Default Application
You can open a file in its default application by entering a simple command:
14] Move File / Folder to different Location
Suppose you want to move TWC12.pdf file to TWC folder in G drive, use below command:
Command to move all files with a specific extension:
To move files starting with a particular letter, say A, command is:
Similarly, you can move a folder using a command like below:
15] Command to Copy Files
You can copy files from one location to another using command:
Hope this article helps you learn some useful commands to manage files and folders through the command line in Windows 11/10.
4 Different Ways to Create a File Using Command Prompt on Windows
Windows file creation simplified
File creation is perhaps one of the most trivial tasks you perform on your computer almost every day. If you’re a Windows user, you probably do it via the graphical interface.
Although this approach may seem easier in practice, especially if you’ve no previous experience with CLI, it has its shortcomings. Unlike the CLI way, which provides a quick and efficient way to create files on your computer, a GUI approach involves multiple steps, making it time-consuming and inefficient.
So here’s a guide to walk you through the different methods you can use to create a file in Windows (be it Windows 11, Windows 10, Windows 8, or Windows 7) with Command Prompt.
Table of Contents
How To Create a File in Windows With Command Prompt
Command Prompt or CMD is a powerful Windows utility that allows you to perform various system operations on your computer with ease. It’s pre-installed on the system and lets you run commands for a multitude of actions, everything from file management to running batch files for task automation.
Creating files happens to be one such task, which you can perform more quickly and efficiently with the CMD prompt than using the File Explorer. But before we jump in and demonstrate how to create a file in Command Prompt, you must know how to navigate the Windows directory structure on it so you can create files in your desired folder.
Navigating Windows File System Using Command Prompt
To navigate the Windows file system with CMD, first, open the Command Prompt. For this, press the Windows + X keyboard shortcut and select Command Prompt from the menu.
Next, enter the dir command to list all the files and directories (or folders) inside a folder. Once you’ve identified the folder you want to open, enter the following command and press Enter:
Eg:
cd Documents
For accessing the root directory:
cd \
While we’re at it, you should also know that you can create directories (or folders) using the Command Prompt. For this, use the cd command to navigate to the folder where you want to create a new folder and use the following syntax:
For example, to create a directory name MyDocs, run:
mkdir MyDocs
Finally, when you’re in the folder where you want to create a file, use any of the following methods to create files with CMD.
Creating a File Using echo Command
The echo command displays messages you type into the CMD window. However, when used with the redirection operator (>), it doubles as a file creation command that creates a file out of your inputted text.
For creating a file using the echo command, open the Command Prompt and enter your command using the following syntax:
echo your_text_here > filename.extension
Once you’ve created the file, verify that it has been created successfully by running:
Eg:
type MyFile.txt
Creating a File Using copy con Command
Unlike the echo command, which takes your input for the content of the file you’re creating at the outset, the copy con command takes a rather different approach.
With it, you only need to give a name to your file initially. And, it then opens the new file in a text editor, where you can populate it with text.
To create a file using copy con, use the syntax below:
copy con filename_with_extension
Eg:
copy con MyFile.txt
It will now put you inside the file in Command Prompt window itself, where you can add your desired text to it. Once you’ve done that, hit Ctrl + Z to save the file and Ctrl + C to exit editing.
Creating a File in Notepad Using Command Prompt
While both methods we’ve mentioned so far work well, they are not ideal for when you want to enter long text paragraphs into your text file. An alternative way to deal with this is to use Notepad, which you usually use to create files, but via the Command Prompt.
For this, enter your command in the CMD window using the following syntax and hit Enter:
notepad filename.extension
For example, if you want to create a file named SampleDoc, you’d run:
notepad SampleDoc.txt
After this, CMD will open the file in Notepad. It will prompt you to create a new file since the file you enter doesn’t exist. Click Yes to accept and create the file.
Now, just like how you would use Notepad, enter your text to the file and hit Ctrl + S to save and Ctrl + W to close the file.
Creating Multiple Files Using Command Prompt
For times when you want to create multiple files inside a folder at once and populate them later, you can use the for loop as shown in the following syntax:
for /l %a in (1 1 10) do type nul > «%a.txt»
…where (1 1 10) tells the CMD to perform the task in the sequence from 1, in steps of 1, up to 10. To create 20 files, replace 10 with 20 in your command.
If you want to add a common name to the start of every file and follow that up with a number, you’d need to use the following syntax:
for /l %a in (1 1 10) do type nul > «filename %a.txt»
…where you need to replace filename with the name you’d like to give your files.
For example, running the command below will create 10 blank files in the following name syntax: MyDoc-1.
for /l %a in (1 1 10) do type nul > «MyDoc-%a.txt»
Besides, if you want to create multiple files with the same text, you can tweak the command to incorporate the same action. To do this, instead of including do type nul, you need to use do echo your_desired_text.
For example, if you want to create 10 files named MyDoc-1.txt, MyDoc-2.txt,… and so on with This is sample text as text, you’d run:
for /l %a in (1 1 10) do echo This is sample text> «MyDoc-%a.txt»
…and it will create them in your current directory.
Efficiently Creating Files on Windows With Command Prompt
The methods listed in this guide should help you with pretty much all sorts of file creation tasks in Command Prompt, be it creating single-line text files, multiple paragraph text files, or even multiple files with custom text and file names.
So depending on requirements, you can choose a method accordingly and create files on Windows quickly and efficiently.
FAQs About CMD Create File
1. How do I create a text file in Windows command line?
Creating a text file in Windows is pretty straightforward, and you have a few different ways to go about it. If all you need is a simple one-line text file, you can use the echo command, whereas if you want to create a text file with multiple lines of text, you can either use the copy con command or create the file in Notepad. Lastly, if you want to create multiple text files, you can use the for loop.
2. Which command is used to create a file?
Microsoft offers a few different CMD commands to create a file, such as echo and copy con. However, you can also use other methods, wherein you can either create a file in Notepad or run a for loop to generate multiple files with your desired name and file name.
How to Create and Delete a File or Folder with CMD [MiniTool Tips]
This tutorial introduces how to create a file or folder/directory with cmd.exe (Command Prompt) on Windows 10. MiniTool software also provides the professional data recovery software to help you recover any deleted/lost files from computer and various storage devices.
If you are wondering how to create a file or folder using Command Prompt, this tutorial provides a detailed guide.
How to Create a Folder with CMD
Step 1. Open Command Prompt
At the beginning, you can press Windows + R to open Run dialog, type cmd in Run, and press Enter to open Command Prompt in Windows 10.
Step 2. Go to the Folder Location
After you enter into Command Prompt, you are at C:\Users\YourName by default. If you want to create a folder in another directory, you can type cd folder path (replace the folder path with the actual folder location), and press Enter to go to the desired directory.
Tip: If the default folder isn’t your target folder and you want to create a file with cmd in anther folder, you should type the whole folder path and hit Enter.
Learn how to hide a folder or file in Windows 10 using CMD. Check how to hide a folder/file or show hidden files Windows 10 with attrib command line.
Step 3. Create a Folder with CMD
Then you can type mkdir folder name (replace folder name with the exact name of the folder you want to create), for instance, mkdir work. After you type the command line, you should press Enter key to run this command to create the folder.
How to Delete a Folder in Command Prompt
Step 1. If you want to delete a folder with CMD, you can follow the same instructions above to enter into Command Prompt and go the directory you want to delete a folder.
Step 2. Then you can type rmdir /s folder name (replace folder name with the exact folder name you’d like to delete), e.g. rmdri /s work. If the folder name has a space between words, you should place the folder name in quotations, e.g. rmdir /s “work 2020”. Finally hit Enter to execute the command, and type “Y” to confirm the action.
How to Create a File with CMD
Step 1. Still, follow the same operation above to open Command Prompt in Windows 10 and go to the folder path in which you want to create a file.
Step 2. Type type nul > filename.fileextension in Command Prompt window and hit Enter, e.g. type nul > work.docx, this will create an empty word file named work.
If you want to create a file with some text in it, you can type echo enter your text here >filename.txt. Replace “enter your text here” with the exact text you want to contain in the file and replace the “filename.txt” with the desired file name and extension. For example, echo How to Create a File with CMD >work.txt.
If you want to create a file with a certain file size in CMD, you can type fsutil file createnew filename.docx 1000, replace filename and 1000 with your preferred file name and file byte size.
Learn how to open a file/folder in Command Prompt (cmd) on Windows 10. Step-by-step guide is included.
How to Delete a File in Command Prompt
Step 1. To delete a file with CMD, you can also open Command Prompt and go to the folder path in Command Prompt.
Step 2. Next you can type dir and press Enter to display all the files in the current folder. Find the file you want to delete.
Step 3. Then you can type del filename.fileextension and press Enter to delete the target file with CMD. You should replace the filename with the exact file name and file extension of the deleting file, e.g. del work.txt. If the file name has spaces, you should put the file name into quotations.
If you receive an error message saying that the file can’t be deleted, you can check: how to force delete a file that cannot be deleted.
Note: Deleting a file/folder using Command Prompt in Windows 10 will permanently delete the file and the deleted file will not go to the Recycle Bin, so please be careful with file deletion with CMD.
How to Recover a Mistakenly Deleted File/Folder
Using CMD to delete a file or folder will permanently delete the file. If you mistakenly delete a file/folder, you can use MiniTool Power Data Recovery – professional free data recovery software for Windows 10/8/7 – to easily recover any deleted/lost files and folder from computer, external hard drive, SSD, USB, SD card, and more in 3 simple steps.
Easy 3 steps to fast recover my files/data for free with best free file recovery software. 23 FAQs for how to recover my files and lost data are included.