简体   繁体   中英

get displayname and office from samaccountname export to csv

How I can get the displayname and the office from a samaccountname list (.txt)? After that I want to save the displaynames and the offices to a .csv file. Here is a approach:

$users = Get-Content C:\TMP\test.txt
foreach ($user in $users)
{
    Get-ADUser -ldapfilter "(samaccountname=$user)" -Property name, office | Select-Object -Property Name, Office

} 

It should look like:

在此处输入图片说明

Hope you can help me?

You are asking for the Export-CSV command but from your comments you might be having issues with placement or your construct of foreach .

Lets try this then

$users = Get-Content C:\TMP\test.txt
$users | ForEach-Object {
    Get-ADUser -ldapfilter "(samaccountname=$_)" -Property name,office | Select-Object -Property Name,office
} | Export-Csv -Path C:\temp\export.csv -NoTypeInformation

Update from comments

Was having issues understanding your output from the comments which was why I wanted more that just a picture of the headers. I don't an issue with this code and the text should be quoted so special characters, like commas, should not be an issue. Please update you question with the text content of a sample file and your PowerShell version in case that is coming into play.

Use the delimiter parameter at the end

$users = Get-Content C:\TMP\test.txt
$users | ForEach-Object {
    Get-ADUser -ldapfilter "(samaccountname=$_)" -Property name,office | Select-Object -Property Name,office
} | Export-Csv -Path C:\temp\export.csv -NoTypeInformation -Delimiter ";"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM