将变量导出到.csv文件

时间:2015-09-08 13:34:27

标签: powershell export-to-csv

我使用PowerShell从AD中提取部门列表:

$departments = Get-ADUser -Filter * -Property Department |
               Where-Object { $_.Department } |
               Select -Expand Department -Unique

在此操作结束时,如果我在控制台上输入$departments,我会按照自己的意愿获得所有部门的名称。但是,如果我尝试使用

将其导出到.csv文件
| Export-Csv H:\TEST\DEPTAAAAA.csv

我明白了

#TYPE System.String
Length
4
9
5
13
13
13

如何导出存储在$departments中的部门名称,以便输出是包含部门名称的.csv文件?

1 个答案:

答案 0 :(得分:5)

SELECT p.username, SUM(case when m.matchday = (select max(matchday) from matchdays) then m.points end) AS last_day_points, SUM(m.points) AS total_points FROM players p INNER JOIN matchdays m ON p.userid = m.userid AND m.season_id = 5 GROUP BY p.userid ORDER BY total_points DESC; cmdlet将输入对象的属性导出为输出CSV的字段。由于您的输入是字符串对象的列表,因此输出是这些对象的属性(SELECT p.username, SUM(case when m.matchday = last_day.matchday then m.points end) AS last_day_points, SUM(m.points) AS total_points FROM players p INNER JOIN matchdays m ON p.userid = m.userid AND m.season_id = 5 CROSS JOIN ( select max(matchday) as matchday from matchdays ) last_day GROUP BY p.userid ORDER BY total_points DESC; )。

如果要通过Export-Csv导出属性,请不要展开属性Length

Department

或如果要将字符串列表写入输出文件,请使用Export-Csv而不是$departments = Get-ADUser -Filter * -Property Department | Where-Object { $_.Department } | Select Department -Unique $departments | Export-Csv 'H:\TEST\DEPTAAAAA.csv'

Out-File