Forum Discussion

dolphin168's avatar
dolphin168
Copper Contributor
Dec 06, 2021

Exchange power shell queries

Hi,

We are using hybrid exchange. we have a user alias list and would like to find the displayname, primarysmtpaddress and output to a file.

Below is the power shell script:

$Users = get-content "D\temp\userlist"

foreach ($u in $ Users)

{

$L = Get-mailbox $u | select alias,promarysmtpaddress, displayname | format-table -autosize

$L

}

$L | export-csv =path "D:\temp\outfile.csv"

 

The export-csv statement is not working and if remove the export-csv statement, the output from the screen comes with the following in every line:
Alias    PrimarysmtpAddress    Displayname

1) Seeking advise how to export the output to a csv file from the script

2) Eliminate "Alias    PrimarysmtpAddress    Displayname" in every line

 

  • You have a typo on the last line, =path should be -path (or you can omit it altogether). To avoid the "header" line, remove format-table, you should never be using this if you want to perform any additional actions with the output, such as export it to CSV. You can also use the -nti switch to remove some additional overhead in the exported CSV.
    You're also exporting each individual entry to a CSV, thus effectively overwriting the output each time. Use the -Append switch to remedy that.
    Lastly, spend some time looking at the cmdlet help and the thousands of articles online showing how to work with it. Getting the basics will help you immensely going forward.

Resources