在 PowerShell 中格式化表格以导出为 CSV [英] Formatting tables in PowerShell to export as CSV

查看:62
本文介绍了在 PowerShell 中格式化表格以导出为 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我睡眠严重不足,压力很大,所以这可能非常简单,但我仍然无法弄清楚.

我想导出各种内容,例如服务、进程、防火墙规则、本地用户,以及我需要的任何 CSV 文件,但每当我将其导出为 CSV 时,当我打开它时,csv 在 Excel 中只有 1 列.

我只希望输出按列分隔,以便我可以在 Excel 中进行简单的概述.

Get-Service |排序对象状态 |格式表-AutoSize |Out-String -Width 10000 |Out-File -Append "C:\temp\Test.csv"

但这是我得到的输出:服务

如果我使用导出 CSV CMD 会发生这种情况:服务

解决方案

PowerShell 的默认 CSV 分隔符是逗号.要使 Excel 正确读取 CSV,请使用 -Delimiter 参数将其更改为分号:

Get-Service |排序状态 |导出-Csv "C:\temp\Test.csv" -NoTypeInfo -Delim ";"

或者使用 -UseCulture 开关(感谢 @JosefZ),它使用与 Excel 相同的基于区域的 ListSeperator 设置:

Get-Service |排序状态 |导出-Csv "C:\temp\Test.csv" -NoTypeInfo -UseCulture

I'm seriously sleep deprived and stressed so it is probably extremely simple yet I still can't manage to figure it out.

I want to get an export of various things like services, processes, firewallrules, local users, whatever I need as a CSV but whenever I export it as such the csv only has 1 column in Excel when I open it.

I just want the output to be separated by columns so I can have a simple overview in Excel.

Get-Service | Sort-Object Status | Format-Table -AutoSize | Out-String -Width 10000 | Out-File -Append "C:\temp\Test.csv"

But this is the output I get: Services

This is what happens if I use a Export-CSV CMD: Services

解决方案

PowerShell's default CSV delimiter is a comma. To make Excel read the CSV properly, change it to a semicolon using the -Delimiter parameter:

Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -Delim ";"

Or use the -UseCulture switch (thanks @JosefZ), which uses the same region-based ListSeperator-setting as Excel:

Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -UseCulture

这篇关于在 PowerShell 中格式化表格以导出为 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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