将变量导出到.csv文件 [英] Exporting a variable to a .csv file

查看:164
本文介绍了将变量导出到.csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I extract a list of departments from AD like this using PowerShell:

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

在此操作结束时,如果我在控制台上输入$departments,则会获得所需的所有部门名称.但是,如果我尝试使用

At the end of this operation, if I input on the console $departments I get all the names of the departments as I wished. But if I try to export this to a .csv file using

| Export-Csv H:\TEST\DEPTAAAAA.csv

我明白了

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

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

How can I export the department names stored in the $departments so the output is a .csv file containing the department names?

推荐答案

Export-Csv cmdlet将输入对象的属性导出为输出CSV的字段.由于您的输入是字符串对象的列表,因此输出是这些对象的属性(Length).

The Export-Csv cmdlet exports the properties of the input objects as the fields of the output CSV. Since your input is a list of string objects the output is the property (Length) of these objects.

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

Don't expand the property Department if you want to export it via Export-Csv:

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

$departments | Export-Csv 'H:\TEST\DEPTAAAAA.csv'

或使用Out-File而不是Export-Csv,如果您想将字符串列表写入输出文件:

or use Out-File instead of Export-Csv if you want to write the list of strings to the output file:

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

$departments | Out-File 'H:\TEST\DEPTAAAAA.csv'

这篇关于将变量导出到.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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