将对象中的数据导出到 csv 中的单个单元格
Exporting data in object to single cell in csv
我需要将存储在以下对象的名称部分中的数据导出到 csv 中的单个单元格。
$localp = Get-Printer | where Shared -eq $false
但每当我这样做时:
$localp.Name | export-csv -path "test.csv"
我明白了:
#TYPE System.String
长度
18
29
22
3
9
尝试
$localp | Select-Object Name | Export-Csv -Path "test.csv" -NoTypeInformation
来自 this answer by Ansgar,
Export-Csv
exports a table of object properties and their values.
Since your script is producing string objects, and the only property
they have is length, that's what you got.
您似乎不需要 header 和单个单元格,
IMO 这不是一个正确的 csv 文件。
如何将打印机彼此分开?
这一行:
('"{0}"' -f ((Get-Printer | where Shared -eq $false).Name -join ',')
创建一个像这样的双引号文本:
"Send To OneNote 2016,Microsoft XPS Document Writer,Microsoft Print to PDF,Fax"
您可以使用 Set-Content
写入文件
$Printers = [PSCustomObject]@{
Printers = (Get-Printer | where Shared -eq $false).Name -join ','}
$Printers |Export-Csv '.\test.csv' -NoTypeInformation
将生成一个文件:
PS> Get-Content .\test.csv
"Printers"
"Send To OneNote 2016,Microsoft XPS Document Writer,Microsoft Print to PDF,Fax"
更改为
-join "`n"
后进行编辑
PS> $printers
Printers
--------
Send To OneNote 2016...
PS> $printers|fl
Printers : Send To OneNote 2016
Microsoft XPS Document Writer
Microsoft Print to PDF
Fax
PS> $printers |export-csv .\test.csv -notype
PS> gc .\test.csv
"Printers"
"Send To OneNote 2016
Microsoft XPS Document Writer
Microsoft Print to PDF
Fax"
我需要将存储在以下对象的名称部分中的数据导出到 csv 中的单个单元格。
$localp = Get-Printer | where Shared -eq $false
但每当我这样做时:
$localp.Name | export-csv -path "test.csv"
我明白了:
#TYPE System.String
长度
18
29
22
3
9
尝试
$localp | Select-Object Name | Export-Csv -Path "test.csv" -NoTypeInformation
来自 this answer by Ansgar,
Export-Csv
exports a table of object properties and their values. Since your script is producing string objects, and the only property they have is length, that's what you got.
您似乎不需要 header 和单个单元格,
IMO 这不是一个正确的 csv 文件。
如何将打印机彼此分开?
这一行:
('"{0}"' -f ((Get-Printer | where Shared -eq $false).Name -join ',')
创建一个像这样的双引号文本:
"Send To OneNote 2016,Microsoft XPS Document Writer,Microsoft Print to PDF,Fax"
您可以使用 Set-Content
$Printers = [PSCustomObject]@{
Printers = (Get-Printer | where Shared -eq $false).Name -join ','}
$Printers |Export-Csv '.\test.csv' -NoTypeInformation
将生成一个文件:
PS> Get-Content .\test.csv
"Printers"
"Send To OneNote 2016,Microsoft XPS Document Writer,Microsoft Print to PDF,Fax"
更改为
-join "`n"后进行编辑
PS> $printers
Printers
--------
Send To OneNote 2016...
PS> $printers|fl
Printers : Send To OneNote 2016
Microsoft XPS Document Writer
Microsoft Print to PDF
Fax
PS> $printers |export-csv .\test.csv -notype
PS> gc .\test.csv
"Printers"
"Send To OneNote 2016
Microsoft XPS Document Writer
Microsoft Print to PDF
Fax"