在Powershell Export-Csv输出中删除标题,用于Select-Object。

huangapple go评论67阅读模式
英文:

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
}

huangapple
  • 本文由 发表于 2023年7月11日 12:56:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定