将内容添加到CSV Powershell [英] Add-Content to CSV Powershell

查看:65
本文介绍了将内容添加到CSV Powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时,我会在单独的列中获得详细信息列表.

When I run the below code I get a list of details in separate columns.

$file = 'c:\output\testing.csv'
   Get-AdUser -filter "DisplayName -eq 'joe bloggs'" | 
   Get-ADUser -Properties * |  
 Select-Object givenName, surname, mail, 
@{ Label = "Country"
Expression = { if ($_.Country) { $_.Country } else { "GB" } }
 ,samaccountname, department, description | Export-csv -path $file

输出(1)如下:

+-----------+---------+-----------------+---------+----------------+------------+-------------+
| givenName | surname |      mail       | Country | samaccountname | department | description |
+-----------+---------+-----------------+---------+----------------+------------+-------------+
| joe       | bloggs  | joe.b@email.com | GB      | bloggsG        | IT         | Support     |
+-----------+---------+-----------------+---------+----------------+------------+-------------+

但是,如果我将Export-CSV修改为 Add-Content ,则输出(2)如下:

However, if I amend the Export-CSV to Add-Content, the output (2) is as below:

+---------------------------------------------------------------------------------------------------------------------------------------+---------+------+---------+----------------+------------+-------------+
|                                                               givenName                                                               | surname | mail | Country | samaccountname | department | description |
| {givenName=Fred; surname=Smith; mail=fred.s@email.com; Country=GB; samaccountname=smithF; department=Facilities; description=cleaner} |         |      |         |                |            |             |
+---------------------------------------------------------------------------------------------------------------------------------------+---------+------+---------+----------------+------------+-------------+

如何使用 Add-Content 显示与第一个表相同的输出?

How do I use Add-Content to show the same output as the first table?

推荐答案

添加内容 用于将非结构化文本附加到文本文件,类似于 Set-Content -您不能使用它将行追加到现有的 CSV文件.

Add-Content is for appending unstructured text to a text file, similar to Set-Content - you cannot use it to append rows to an existing CSV file.

相反,您必须使用 Export-Csv -Append :

Instead, you must use Export-Csv -Append:

... | Export-Csv -Path $file -Append -Encoding Ascii # pick the proper encoding

字符编码警告,自Windows PowerShell v5.1起 [1] :

Character-encoding caveats, as of Windows PowerShell v5.1[1]:

  • -追加

  • Without -Append, Export-Csv uses ASCII(!) encoding - against documented behavior.

  • 这意味着非ASCII字符会音译为文字 ?,即您可能会丢失信息.
  • This means that non-ASCII characters are transliterated to literal ?, i.e., you may lose information.

使用 -追加,奇怪的是,非ASCII字符是 UTF-8 编码的-与文件的原始编码无关.

With -Append, curiously, non-ASCII characters are UTF-8-encoded - irrespective of the file's original encoding.

要获得可预测的行为,请始终明确将 -Encoding Export-Csv 一起使用-带有和不带有 -Append ./strong>

To get predictable behavior, always use -Encoding explicitly with Export-Csv - both with and without -Append.

[1]跨平台PowerShell Core 版本不受影响,因为默认情况下始终使用无BOM的UTF-8编码.

[1] The cross-platform PowerShell Core edition is not affected, because it consistently uses BOM-less UTF-8 encoding by default.

这篇关于将内容添加到CSV Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆