Table of Contents
Using New-Item
Cmdlet
Use the New-Item
cmdlet to create an empty .txt
file in PowerShell.
1 2 3 |
New-Item -Path "E:\NewFolder" -Name "File1.txt" -ItemType File |
1 2 3 4 5 6 7 |
Directory: E:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/27/2022 10:51 PM 0 EmptyFile.txt |
We used the New-Item
cmdlet with -Path
, -Name
, and -ItemType
parameters to create an empty .txt
file using Windows PowerShell. Let’s break down the above command to understand it more clearly.
The New-Item
cmdlet creates a new item and sets its value by adding initial content or data to the file. We can use this cmdlet to create a different types of items depending on what location we are using for the items. For instance, in registry
, the New-Item
creates registry entries and keys, and if we are in FileSystem
, then the New-Item
creates folders and files.
The -Name
parameter is used to specify the new item’s name, while -Path
is used to write the path of the location where our new file will reside. We used "File1.txt"
as the name of the new item and "E:\NewFolder"
as the location’s path.
Remember, if we do not use the -Path
parameter, the New-Item
cmdlet will save the newly created file in the current directory. We can write the new item’s name in the -Name
or -Path
parameter. See the following examples.
1 2 3 |
New-Item -Path "E:\NewFolder\File1.txt" -ItemType File |
1 2 3 |
New-Item -Path "E:\NewFolder" -Name "File1.txt" -ItemType File |
Note that the names of the new items we pass using the -Name
parameter will be created relative to the -Path
parameter’s value. For the New-Item
cmdlet, the -Path
behaves like other cmdlets’ LiteralPath
parameter.
The worth noting point is that wildcard characters will not be interpreted. So, for instance, we can not create a .txt
file with a name which contains an asterisk (*
).
The -ItemType
parameter is used to specify the new item’s type. Now, what values can we use with this parameter? It depends on the current provider. For instance, if we are in a location which is in FileSystem
then, we can use File
, Directory
, SymbolicLink
, Junction
, HardLink
as values for the -ItemType
parameter.
Use the ni
Alias
Use ni
to create an empty .txt
file in PowerShell.
1 2 3 |
ni -Path "E:\NewFolder" -Name "File2.txt" -ItemType File |
1 2 3 4 5 6 7 |
Directory: E:\NewFolder Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 12:14 AM 0 File2.txt |
The ni
is not a different cmdlet, but it is a built-in alias for the New-Item
and works as New-Item
does. You can go back to the previous section to learn about the New-Item
cmdlet, its parameters, and its working.
As we saw, we got some informational text in the above output, including mode, length, file name, and the last update date/time. What if we want to avoid getting this type of information and are only concerned with the file’s creation? In that case, the following solution will work for us.
Using Out-File
Cmdlet
Use the Out-File
cmdlet to create an empty file (.txt
) in PowerShell.
1 2 3 |
Out-File -FilePath "File3.txt" |
Here, we used the Out-File
cmdlet with the -FilePath
parameter to create a .txt
file without content. We can also use this command as Out-File -FilePath "E:\NewFolder\File3.txt"
to specify the path; otherwise, the File3.txt
would be created in the current directory.
The primary use of Out-File
is to send the output (received from the previous process) to a file. Look at the following example, where we get details of all the currently running processes using the Get-Process
cmdlet and forward this information to Out-File
using the pipe sign (|
).
Now, the Out-File
will write the received data to the specified text file we can print on the PowerShell using the Get-Content
cmdlet. Note that the Out-File
will create the File3.txt
file if it does not exist already.
1 2 3 4 |
Get-Process | Out-File -FilePath "File3.txt" Get-Content -Path ".\File3.txt" |
1 2 3 4 5 6 |
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 165 9 2532 2808 2376 0 AdminService 426 24 19724 19752 2.16 10712 27 ApplicationFrameHost |
Now, think of Out-File
, where we write nothing to the file. What does it mean to write nothing to a file? It means we are passing null
using $null
as follows. Here, $null
is an automatic variable representing null
in PowerShell.
1 2 3 4 |
$null | Out-File -FilePath "File3.txt" Get-Content -Path ".\File3.txt" |
For the above code, the Get-Content
cmdlet will show nothing because the File3.txt
file is empty. Are you thinking that if we are writing nothing in the File3.txt
file, then why not create a file without writing anything?
Yes, you got it right! Therefore, we can omit the $null
variable and use the Out-File
as follows to create an empty .txt
file.
1 2 3 |
Out-File -FilePath "File3.txt" |
Using the fsutil file
Command
Use the fsutil file
command to create an empty .txt
file in PowerShell.
1 2 3 |
fsutil file createnew "File4.txt" 1000 |
1 2 3 |
File E:\File4.txt is created |
We used the fsutil file
command to create an empty File4.txt
file in the current directory. We used this command because we were in a situation where we had to specify the length
of the file while creating it. Remember, the fsutil file
command will not work if the value for the length
parameter is missing (in our case, the length
is 1000
).
Note that the fsutil file
command can do various things; for instance, it can find a file by username, set a short name of a file, set a valid data length of a file, or create a new file, etc. based on what parameter is passed.
For example, in our case, the fsutil file
created an empty text file due to using the createnew
parameter. On the other hand, it can find the files belonging to a particular user if used with the findbyid
parameter.
The last numerical value (1000
) in the above code snippet is used for the length
parameter, which is used to specify the valid data length of a file.
Using Text Editor
Use the text editor in PowerShell to create an empty .txt
File.
1 2 3 |
notepad "File5.txt" |
As soon as we hit the Enter key after writing the above command, an empty File5.txt
will be created (if it does not exist) and opened as well. We used notepad
as a text editor; you can use any of your choices, for instance, nano
, vim
, etc.
Further reading:
Using echo
Command
Use the echo
command with greater than sign (>
) in the PowerShell to create an empty .txt
File.
1 2 3 |
echo "" > "File6.txt" |
The primary use of echo
is to write, but we are writing an empty string to a file. It is the most basic command, fine for learning purposes but not recommended for professional use where you are required to create a brand new but empty file.
So, we have learned various approaches for creating an empty .txt
file in PowerShell, but all the approaches were focused on creating a single file. So, let’s learn the use of the for
loop to create n
empty text files.
Using for
Loop
To create an empty text file in PowerShell:
- Use a
for
loop, which iterates from 1(including) to 11 (excluding). - Create a variable containing the path and file name.
- Use the
New-Item
cmdlet to create an empty text file.
1 2 3 4 5 6 |
for($i = 1; $i -lt 11; $i++){ $path = "E:\File{0:00}.txt" -f $i New-Item -Path $path } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Directory: E:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 2:00 AM 0 File01.txt -a---- 12/28/2022 2:00 AM 0 File02.txt -a---- 12/28/2022 2:00 AM 0 File03.txt -a---- 12/28/2022 2:00 AM 0 File04.txt -a---- 12/28/2022 2:00 AM 0 File05.txt -a---- 12/28/2022 2:00 AM 0 File06.txt -a---- 12/28/2022 2:00 AM 0 File07.txt -a---- 12/28/2022 2:00 AM 0 File08.txt -a---- 12/28/2022 2:00 AM 0 File09.txt -a---- 12/28/2022 2:00 AM 0 File10.txt |
For this example, we used to loop over the given range, which is 1
to 11
, where 11
is not included. Next, we created a $path
variable, which holds the location’s path and file’s name formatted using the -f
operator.
Here, the formatting operator (-f
) was used to concatenate the string with the value of the $i
variable. Finally, we used the New-Item
cmdlet, discussed in the first section, to create a new text file.
That’s all about how to create empty file in PowerShell.