英文:
Removing the header in Powershell Export-Csv output for Select-Object
问题
当前输出:
name
clark kent
mickey mouse
jolly rancher
期望输出:
clark kent
mickey mouse
jolly rancher
我已尝试将 Select-Object -Property name
替换为 Select-Object -ExpandProperty name
,但它不会产生其他答案中所述的期望输出。
英文:
I'm outputting a list of members from an AD group to a csv file. Right now it's outputting with the header 'name'. I currently want to have the output without the header.
This is my code:
$groupNames = @('Group1', 'Group2', 'Group3', 'Group4')
foreach ($group in $groupNames) {
$csvPath = "$group.csv"
$groupdn = (Get-ADGroup $group).DistinguishedName
Get-ADUser -LDAPFilter "(memberof=$groupdn)" |
Select-Object name |
Export-Csv -Path $csvPath -NoTypeInformation
}
Current Output:
name
clark kent
mickey mouse
jolly rancher
Desired Output:
clark kent
mickey mouse
jolly rancher
I've tried replacing Select-Object -Property name
with Select-Object -ExpandProperty name
but it does not give the desired output found in other answers on here.
答案1
得分: 3
你现在寻找的不再是Csv文件,而是一个包含AD对象名称列表的纯文本文件,因此Export-Csv
不再是你的用例的正确命令,你应该将它更改为Set-Content
,并展开对象的.Name
属性,而不是选择它们:
$groupNames = @('Group1', 'Group2', 'Group3', 'Group4')
foreach ($group in $groupNames) {
$csvPath = "$group.csv"
$groupdn = (Get-ADGroup $group).DistinguishedName
Get-ADUser -LDAPFilter "(memberof=$groupdn)" |
ForEach-Object name |
Set-Content $plainTextPath
}
英文:
What you are looking for is no longer a Csv but a plain text file with a list of AD object names thus Export-Csv
is no longer the right cmdlet for your use case, you should change it to Set-Content
and expand the objects .Name
attribute instead of selecting them:
$groupNames = @('Group1', 'Group2', 'Group3', 'Group4')
foreach ($group in $groupNames) {
$csvPath = "$group.csv"
$groupdn = (Get-ADGroup $group).DistinguishedName
Get-ADUser -LDAPFilter "(memberof=$groupdn)" |
ForEach-Object name |
Set-Content $plainTextPath
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论