Powershell ConvertTo-CSV自定义标头 [英] Powershell ConvertTo-CSV custom headers

查看:136
本文介绍了Powershell ConvertTo-CSV自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将数据转换为CSV时,Powershell是否支持使用自定义标头名称?我正在从Active Directory转换用户数据,并希望在标题中使用属性名称以外的其他名称.

Does Powershell support using custom header names when converting data to CSV? I'm convertering user data from Active Directory and want to use other names in the headers than the property names.

我的代码:

$adusers | select  givenName,sn,telephoneNumber,SamAccountName,physicalDeliveryOfficeName,Title,mobile | ConvertTo-Csv -Delimiter ';' | Out-File -FilePath $thefilepath

推荐答案

有很多方法可以给这只猫蒙皮. 您可以从输出中创建新的自定义对象,并根据需要命名属性:

There are many ways to skin this cat. You can create new custom objects from the output and name the properties what you'd like:

$adOutput | Foreach-Object{
   New-Object PSObject -Property @{
      UserName = $_.SamAccountName
      ...
   }
} | Export-Csv ...

您也可以使用计算出的属性来做到这一点:

You can also do that using calculated properties:

$adOutput | Select Name,@{Name='UserName';Expression={$_.SamAccountName}} | Export-Csv ...

这篇关于Powershell ConvertTo-CSV自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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