带有示例的PowerShell Export-CSV命令教程

本文是一篇关于PowerShell Export-CSV命令的教程,详细介绍了如何导出到CSV文件,设置分隔符,选择写入属性,附加到现有文件,不覆盖文件,覆盖只读文件以及设置CSV文件编码等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Command-separated Value or CSV files are a very popular type of text files which are used for different purposes. What makes CSV special is the ability to store structured data by delimiting the values or columns with comma. CSV files are compatible with Excel files to import and export.

Command-separated ValueCSV文件是一种非常流行的文本文件,用于不同目的。 CSV的特殊之处在于它能够通过用逗号分隔值或列来存储结构化数据。 CSV文件与导入和导出的Excel文件兼容。

导出到逗号分隔的CSV文件 (Export To Comma Delimited CSV File)

We will start with a simple example. Get-Process command is used to list currently running proccesses with different information like Handle, NPM, CPU,ID, ProcessName etc. We can use the command Export-CSV in order to store these process information delimited with command.

我们将从一个简单的例子开始。 Get-Process命令用于列出当前运行Get-Process具有不同的信息,例如Handle,NPM,CPU,ID,ProcessName等。我们可以使用Export-CSV命令来存储这些用命令定界的过程信息。

PS> Get-Process

Now we can put these running processes information into a CSV file by redirecting Get-Process output. We will provide the file name with the option -Path. The file name will be ProcessList.csv.

现在,我们可以通过重定向Get-Process输出将这些正在运行的进程信息放入CSV文件中。 我们将为文件名提供-Path选项。 文件名将为ProcessList.csv。

PS> Get-Process | Export-CSV -Path ProcessList.csv

Alternatively we can provide different directory with the option -Path. In the following command we will put the ProcessList.csv file into the C:\Users\İsmail Baydan\Desktop.

或者,我们可以使用-Path选项提供不同的目录。 在以下命令中,我们将把ProcessList.csv文件放入C:\Users\İsmail Baydan\Desktop

PS> Get-Content 'C:\Users\İsmail Baydan\Desktop\ProcessList.csv'

设置CSV文件的分隔符 (Set Delimiter For CSV File)

Delimiters are an important part of the CSV files. By default, CSV files use the command as a delimiter. But we can change delimiter whatever we want. We will use the option -Delimeter in order to set a different delimiter character. In the following example, we will set the equal sign = as the delimiter. Putting the delimiter inside the double quotes is a very good way to prevent errors.

分隔符是CSV文件的重要组成部分。 默认情况下,CSV文件使用命令作为定界符。 但是我们可以根据需要更改定界符。 我们将使用选项-Delimeter来设置其他定界符。 在下面的示例中,我们将等号=设置为定界符。 将分隔符放在双引号内是防止错误的一种很好的方法。

PS> Get-Process | Export-CSV -Delimiter "=" -Path ProcessList.csv

导出到以分号分隔的CSV文件 (Export To Semicolon Delimited CSV File)

The comma is the standard delimiter for the CSV files. But we can use different delimiters in order to separate columns. In some cases, a comma can be used inside to content we want to put into the CSV file. This will crash the CSV file because delimiter and content will be same and it will be error-prone to parse the content with the delimiter. So good alternative to the command delimiter is semicolon which is rarely used in CSV file contents. In the following example, we will set semicolon as delimiter.

逗号是CSV文件的标准分隔符。 但是我们可以使用不同的定界符来分隔列。 在某些情况下,可以在我们要放入CSV文件的内容中使用逗号。 这将使CSV文件崩溃,因为定界符和内容相同,并且使用定界符解析内容很容易出错。 因此,命令分隔符的很好替代方法是分号,该分号在CSV文件内容中很少使用。 在下面的示例中,我们将分号设置为定界符。

PS> Get-Process | Export-CSV -Delimiter ";" -Path ProcessList.csv

选择要写入CSV文件的属性 (Select Properties To Write CSV File)

Up to now we have worked with the Get-Process command which will list all properties of the currently running processes. In some cases we do not need to put all properties into the CSV file. We can select which properties can be put into the CSV file. We will use the command Select-Object and its option -Property like below.

到目前为止,我们已经使用了Get-Process命令,该命令将列出当前正在运行的进程的所有属性。 在某些情况下,我们不需要将所有属性都放入CSV文件中。 我们可以选择可以将哪些属性放入CSV文件。 我们将使用命令Select-Object及其选项-Property如下所示。

PS> Get-Process | Select-Object -Property ProcessName,Id | Export-CSV -Path ProcessList.csv
Select Properties To Write CSV File
选择要写入CSV文件的属性

附加到现有的CSV文件(Append To The Existing CSV File)

Another use case for the command Export-CSV is adding new CSV content into the existing CSV content. We can use the option -Append which will add new CSV content into the existing file.

命令Export-CSV的另一个用例是将新CSV内容添加到现有CSV内容中。 我们可以使用选项-Append将新的CSV内容添加到现有文件中。

PS> Get-Process | Export-CSV -Delimiter -Append -Path ProcessList.csv

不覆盖现有的CSV文件 (Do Not Overwrite Existing CSV File)

By default command Export-CSV will overwrite on the existing file without any warning. We can prevent this actions with the option -NoClobber which will do not over-write existing file and print and error realted given file allready exists.

默认情况下,Export-CSV命令将覆盖现有文件,而不会发出任何警告。 我们可以使用-NoClobber选项来阻止此操作,该选项不会覆盖现有文件,并且在已存在文件的情况下进行打印并显示错误。

PS> Get-Process | Export-CSV -NoClobber -Path ProcessList.csv
Do Not Overwrite Existing CSV File
不覆盖现有的CSV文件

覆盖只读文件(Overwrite Read-Only Files)

By default read-only files can be edited, appended by default. But Export-CSV command can change the read-only attribute for editing and then revert back to read-only. We will force to write operation with the option -Force like below.

默认情况下,只读文件可以编辑,默认情况下会附加。 但是Export-CSV命令可以更改只读属性以进行编辑,然后恢复为只读。 我们将强制使用-Force选项编写操作,如下所示。

PS> Get-Process | Export-CSV  -Append -Force -Path ProcessList.csv

设置CSV文件编码 (Set CSV File Encoding)

As CSV file is a text file it has an encoding option. By default, the UTF-8 is the default encoding for the text and CSV files. But in some cases, we may need to change the default encoding and set specific encoding. We can use the option -Encoding and provide an encoding name like ASCII. In the following example, we will set the CSV file encoding as ASCII.

由于CSV文件是文本文件,因此具有编码选项。 默认情况下,UTF-8是文本和CSV文件的默认编码。 但是在某些情况下,我们可能需要更改默认编码并设置特定的编码。 我们可以使用-Encoding选项,并提供ASCII之类的编码名称。 在以下示例中,我们将CSV文件编码设置为ASCII。

PS> Get-Process | Export-CSV -Encoding ASCII -Path ProcessList.csv

Below are popular and available encoding formats which can be used with the Export-CSV.

以下是可以与Export-CSV一起使用的流行且可用的编码格式。

  • ASCII

    ASCII码
  • BigEndianUnicode

    BigEndianUnicode
  • OEM

    OEM代工
  • Unicode

    统一码
  • UTF7

    UTF7
  • UTF8

    UTF8
  • UTF8BOM

    UTF8BOM
  • UTF8NoBOM

    UTF8NoBOM
  • UTF32

    UTF32

翻译自: https://www.poftut.com/powershell-export-csv-command-tutorial-with-examples/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值