将内容添加到 CSV Powershell [英] Add-Content to 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?
推荐答案
Add-Content
用于将非结构化文本附加到文本文件,类似于 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]:
没有
-Append
,Export-Csv
使用 ASCII(!) 编码 - 反对记录的行为.
Without
-Append
,Export-Csv
uses ASCII(!) encoding - against documented behavior.
- 这意味着非 ASCII 字符被转写为 literal
?
,即,您可能会丢失信息.
- This means that non-ASCII characters are transliterated to literal
?
, i.e., you may lose information.
使用 -Append
,奇怪的是,非 ASCII 字符是 UTF-8 编码的 - 与文件的原始编码无关.
With -Append
, curiously, non-ASCII characters are UTF-8-encoded - irrespective of the file's original encoding.
要获得可预测的行为,请始终将 -Encoding
明确与 Export-Csv
一起使用 - 无论是否带有 -Append
.
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屋!