cmdlet_如何使用Out-File和Export-Csv Cmdlet输出到Powershell中的文件?

cmdlet

cmdlet

Out-File is a cmdlet provided by Powershell core libraries. This cmdlet provides features to write created output into a file in the file system. In this tutorial, we will look at different ways to write output into a file with Out-File and Export-CSV cmdlets.

Out-File是Powershell核心库提供的cmdlet。 此cmdlet提供的功能可将创建的输出写入文件系统中的文件。 在本教程中,我们将探讨使用Out-FileExport-CSV cmdlet将输出写入文件的不同方法。

写入文件 (Write Into A File)

One of the basic usage scenario of out-file is writing the output into a file without changing it. In this example we will write the output of Get-Process command into a file named process.txt by piping it. The file name can be specified as full path like C:\user\process.txt

外文件的基本使用方案之一是将输出写入文件而不更改它。 在此示例中,我们将通过管道将Get-Process命令的输出写入名为process.txt的文件中。 可以将文件名指定为完整路径,例如C:\user\process.txt

PS> Get-Process | Out-File process.txt
Write Into A File
Write Into A File
写入文件

追加到现有文件(Append Into Existing File)

If we just want to add new lines and do not want to remove existing data we should add new data with -Append option like below.

如果我们只想添加新行而不希望删除现有数据,则应使用-Append选项添加新数据,如下所示。

PS> Get-Process | Out-File -Append process.txt

写为CSV (Write As CSV)

We can write created output into a file is CSV format. CSV format delimits the columns with comma. We will use Export-Csv cmdlet for this operation. This will take more time than raw write into file because row will be separated and each column will be delimited with command.

我们可以将创建的输出写入CSV格式的文件中。 CSV格式以逗号分隔列。 我们将使用Export-Csv cmdlet进行此操作。 这将比原始写入文件花费更多的时间,因为行将被分隔并且每列将由命令定界。

PS> Get-Process | Export-Csv test.txt
Write As CSV
Write As CSV
写为CSV

写入文件和控制台 (Write Both File and Console)

Another most used feature is while writing output into a file printing in the console too. This will provide verbose information about output. We will use Tee-Object which is inherited from Linux operating system.

另一个最常用的功能是在控制台中将输出写入文件打印时。 这将提供有关输出的详细信息。 我们将使用从Linux操作系统继承的Tee-Object

In this example we get dir command output into console and file named dirs.txt

在此示例中,我们将dir命令输出输出到控制台和名为dirs.txt文件中

PS> Dir | Tee-Object -f dirs.txt
Write Both File and Console
Write Both File and Console
写入文件和控制台
LEARN MORE  How To Download, Install, and Use Python Idle Editor?
了解更多如何下载,安装和使用Python Idle Editor?

翻译自: https://www.poftut.com/how-to-output-to-a-file-in-powershell-with-out-file-and-export-csv-cmdlet/

cmdlet

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值