通过 Windows Powershell 创建新文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45446285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:55:14  来源:igfitidea点击:

Creating new file through Windows Powershell

windowspowershellcreatefile

提问by JR Sahoo.'JS'

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?

我在谷歌上搜索了以下问题,但找不到任何答案。有人可以帮助我吗?通过 Windows Powershell 创建新文件的命令是什么?

采纳答案by Jo?e Stro?er

To create file using echo

使用 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 创建文件

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.

Fsutil 只能由管理员使用。对于非管理员用户,它会引发以下错误。

c:\>fsutil file /?

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

FSUTIL 实用程序要求您具有管理权限。c:>

Hope this helps!

希望这可以帮助!

回答by J. D.

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.

其中“C:\scripts\new_file.txt”是包含文件名和扩展名的完全限定路径。

Taken from TechNet article

摘自TechNet 文章

回答by Gajendra D Ambi

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

street smart(快速,肮脏但有效):(可能会更改文件并添加一个可能导致编译器失败的不可见字符)

$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

缺少 -path 参数意味着它会在当前工作目录中创建它

回答by Josh H

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

这是在 Powershell 中创建空白文本文件的另一种方法,它允许您指定编码。

First example

第一个例子

For a blank text file:

对于空白文本文件:

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

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

如果没有-encoding ascii,Powershell 默认为 Unicode。您必须指定ascii是否希望它可以被其他来源读取或编辑。

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.txtwith Some Text on first line.

这将替换任何文本是filename.txtSome 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 -Appendleaves the current contents of filename.txtalone and adds Some More Text after the old textto the end of the file, leaving the current content intact.

指定-Append叶子的当前内容filename.txt单独并添加Some More Text after the old text到文件末尾,离开当前的内容不变。

回答by themefield

ni filename.txt

Replace filename.txtwith your file .

替换filename.txt为您的文件。

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

我发现这是该问题的最简单答案,有关更多详细信息,请参阅其他答案。

回答by user2263572

                                                       # 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