Creating new file through Windows Powershell
Asked Answered
F

10

70

I have googled for the below question, but could not find any answer. Can someone help me on this; What is the command to create a new file through Windows Powershell?

Fotina answered 1/8, 2017 at 19:34 Comment(2)
what type of file are you trying to create? Or in other words what file extension? Like .txt .docx etc...Yokel
@user6811411 These sorts of comments never seem to age well. This question is now the first result when searching "powershell create empty file" on Google.Paxwax
V
61

To create file using echo

echo some-text  > filename.txt

Example:

C:\>echo This is a sample text file > sample.txt
C:\>type sample.txt
This is a sample text file
C:\>

To create file using fsutil

fsutil file createnew filename number_of_bytes

Example:

fsutil file createnew sample2.txt 2000
File C:\sample2.txt is created
C:\data>dir
01/23/2016  09:34 PM     2,000 sample2.txt
C:\data>

Limitations

Fsutil can be used only by administrators. For non-admin users it throws up below error.

c:\>fsutil file /?

The FSUTIL utility requires that you have administrative privileges. c:>

Hope this helps!

Vying answered 1/8, 2017 at 19:41 Comment(1)
Please don't suggest the use of echo - an alias for the rarely needed Write-Output cmdlet - because it is a bad habit to promote in the PowerShell world. Newcomers need to be guided towards PowerShell's implicit output model: 'some-text' > filename.txt. If your code is meant to be cmd.exe code (it also works as that), please note the the question is tagged powershell.Fanaticize
F
75

I'm guessing you're trying to create a text file?

New-Item c:\scripts\new_file.txt -type file

Where "C:\scripts\new_file.txt" is the fully qualified path including the file name and extension.

Taken from TechNet article

Fari answered 1/8, 2017 at 19:37 Comment(3)
++, but note that you're not creating a text file, you're creating a zero-byte (empty file) - which has no inherent type.Fanaticize
This should be the answer. Get, Start, New, and etc is new era of cmdlet that's great for sysadmin. Just left old command behind.Gopher
how to create the file and every directory within the path?Intersexual
V
61

To create file using echo

echo some-text  > filename.txt

Example:

C:\>echo This is a sample text file > sample.txt
C:\>type sample.txt
This is a sample text file
C:\>

To create file using fsutil

fsutil file createnew filename number_of_bytes

Example:

fsutil file createnew sample2.txt 2000
File C:\sample2.txt is created
C:\data>dir
01/23/2016  09:34 PM     2,000 sample2.txt
C:\data>

Limitations

Fsutil can be used only by administrators. For non-admin users it throws up below error.

c:\>fsutil file /?

The FSUTIL utility requires that you have administrative privileges. c:>

Hope this helps!

Vying answered 1/8, 2017 at 19:41 Comment(1)
Please don't suggest the use of echo - an alias for the rarely needed Write-Output cmdlet - because it is a bad habit to promote in the PowerShell world. Newcomers need to be guided towards PowerShell's implicit output model: 'some-text' > filename.txt. If your code is meant to be cmd.exe code (it also works as that), please note the the question is tagged powershell.Fanaticize
B
26

street smart (quick, dirty but works): (might change the file and add an invisible character which might cause the compiler to fail)

$null > file.txt
$null > file.html

Textbook method:

New-Item -path <path to the destination file> -type file

example:

New-Item -path "c:\" -type file -name "somefile.txt"

OR

ni file.xt -type file

absence of -path parameter means it creates it in the current working directory

Beore answered 24/6, 2018 at 11:38 Comment(1)
So many great options, especially short-hand cmd ni with working directoryRacketeer
T
23
ni filename.txt

Replace filename.txt with your file .

I found this the simplest answer to the question, and refer to other answers for more details.

Told answered 19/2, 2019 at 20:2 Comment(3)
Quick thing to point out here is that ni is a default alias for the New-Item command. Alias information for a specific command can be view via the gal -definition command, e.g. gal -definition New-Item.Engraft
I really liked this answer. Also ni (New-Item) creates the file with UTF-8 encoding and echo creates it with ucs-2 le bom(UTF-16) encoding. Unless you change the encoding, the files created with echo will be twice the size.Grandchild
This is easiest. Note that when running this, my powershell asked for type, I put in filePlatina
P
9

Here is another way to create a blank text file in Powershell which allows you to specify the encoding.

First example

For a blank text file:

Out-File C:\filename.txt -encoding ascii

Without -encoding ascii, Powershell defaults to Unicode. You must specify ascii if you want it to be readable or editable by another source.

Overwriting the file with new text:

"Some Text on first line" | Out-File C:\filename1.txt -encoding ascii

This replaces whatever text is in filename.txt with Some Text on first line.

Appending text to the current file contents:

"Some More Text after the old text" | Out-File C:\filename1.txt -encoding ascii -Append

Specifying -Append leaves the current contents of filename.txt alone and adds Some More Text after the old text to the end of the file, leaving the current content intact.

Protest answered 9/12, 2018 at 20:22 Comment(0)
L
3

As many have already pointed out, you can create files with the New-File command.
This command has a default alias set to ni but if you're used to unix commands you can create your own custom command easily.

Create a touch command to act as New-File like this:

Set-Alias -Name touch -Value New-Item

This new alias will allow you to create new files like so:

touch filename.txt

This would make these 3 commands equivalent:

New-Item filename.txt
ni filename.txt
touch filename.txt

Keep in mind that for this to be persistent, you should add the alias to your powershell profile. To get it's location simply run $profile on ps. If you want to edit it directly, run code $profile (for VSCode), vim $profile (for vim) or whatever.

Letha answered 18/11, 2020 at 22:36 Comment(0)
F
2

Another way to do it (method I like)

New-Item -ItemType file -Value 'This is just a test file' -Path C:\Users\Rick\Desktop\test.txt

Source: New-Item

Feather answered 19/11, 2020 at 10:12 Comment(0)
I
2

Use the New-item cmdlet and your new file name.

New-item <filename>

Example:

New-item My_newFile.txt
Inbreeding answered 15/8, 2022 at 13:44 Comment(0)
G
1
                                                       # encodings:

New-Item file.js -ItemType File -Value "some content"  # UTF-8

"some content" | Out-File main.js -Encoding utf8       # UTF-8-BOM

echo "some content" > file.js                          # UCS-2 LE BOM
Gabrielgabriela answered 31/1, 2020 at 14:1 Comment(0)
A
1

The easiest way in any command line:

"" > path/to/file/filename.extension
Abortionist answered 30/3 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.