Solved

Powershell Exchange Script - Export Forwarded Email

Posted on 2014-12-12
3
265 Views
Last Modified: 2014-12-15
Hi Experts,

I have the following script:

Get-Mailbox -Filter {ForwardingAddress -ne
$null} | foreach {$recipient = $_; $forwardingsmtp = (Get-Recipient $_.ForwardingAddress).PrimarySmtpAddress; 
Write-Host $recipient.Name, $forwardingsmtp, $recipient.DeliverToMailboxAndForward } 

Open in new window


I need the output to outputted to a CSV showing the following:

User, User Primary SMTP and Forwarding SMTP address.

I am only able to view this on the command view and cannot seem to output this to a CSV.

Any help will be appreciated.

Regards,

introlux

Using: Exchange 2007 Standard
0
Comment
Question by:introlux
3 Comments
 
LVL 8

Expert Comment

by:tshearon
Comment Utility
Have you tried just adding another pipe at the end and using the export-csv function?

| export-csv c:\report.csv
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
That won't work, because the Write-Host generates a single string without properties and delimiters.
Trivial, self-formatting way:
Get-Mailbox -Filter {ForwardingAddress -ne $null} |
  % {
    $recipient = $_
    $forwardingsmtp = (Get-Recipient $_.ForwardingAddress).PrimarySmtpAddress
    Write-Host "$($recipient.Name),$forwardingsmtp,$($recipient.DeliverToMailboxAndForward)"
  } | out-file 'text.csv'

Open in new window

Better:
Get-Mailbox -Filter {ForwardingAddress -ne $null} |
  % {
    New-Object PsObject -Property @{
      recipient = $_.Name
      forwardingsmtp = (Get-Recipient $_.ForwardingAddress).PrimarySmtpAddress
      DeliverToMailboxAndForward = $_.DeliverToMailboxAndForward
    }
  } | export-csv -notype 'text.csv'

Open in new window

0
 

Author Closing Comment

by:introlux
Comment Utility
Spot on!!

Thanks,

introlux
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now