Creating a new file via Windows Powershell

I have googled for below question but couldn't find any answer. Can anyone help me on this; What is the command to create a new file via Windows Powershell?

+18


source to share


4 answers


To create a 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 a 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 only be used by administrators. For non-admin users, it gets thrown below the error.

c:\>fsutil file /?

      

FSUTIL requires administrative privileges. C:>

Hope this helps!

+23


source


I am assuming you are trying to create a text file?

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

      



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

Taken from TechNet Article

+28


source


Street Smart (fast, messy, but works): (may modify the file and add an invisible character, which could cause the compiler to crash)

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

      

Tutorial 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

      

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

+9


source


Here's another way to create an empty text file in Powershell that allows you to specify the encoding.

First example

For an empty text file:

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

      

Without -encoding ascii

Powershell, it uses Unicode by default. You must specify ascii

if you want it to be readable or editable by another source.

We overwrite the file with new text:

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

      

This replaces any text filename.txt

onSome Text on first line.

Adding text to the current content of the file:

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

      

The hint -Append

leaves the current content filename.txt

alone and appends Some More Text after the old text

to the end of the file, leaving the current content unchanged.

0


source







All Articles