• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1504
  • Last Modified:

Redirect .ps1 output to .csv

I need to figure out how to export the results of this command to a .csv file.

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

I have tried adding the following at the end of the command

 | Export-Csv file.csv

and that didn't work.  I also tried

myfile.ps1 | %{write-host $_;$_} | out-file mylog.log

to no avail.  I've looked everywhere and can't figure this out so I'm coming to my fellow experts.  How can I successfully output this .ps1 to a .csv file?

Thanks in advance!
0
JesusSardinas
Asked:
JesusSardinas
  • 15
  • 6
  • 5
  • +2
4 Solutions
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
myfile.ps1 | %{write-host $_;$_} > C:\mylog.log

- Rancy
0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
Get-Mailbox -Filter {ForwardingAddress -ne $null} | foreach {$recipient = $_; $forwardingsmtp = (Get-Recipient $_.ForwardingAddress).DisplayName; Write-Host $recipient.Name, $forwardingsmtp, $recipient.DeliverToMailboxAndForward} > C:\Mailbox.csv

- Rancy
0
 
JesusSardinasAuthor Commented:
Rancy thanks for the attempt but neither one of those solutions worked.  Those are the same things I have tried and they didn't work.  I went as far as copying your code (with the correct file name) and it still didn't work.  The data continues to appear on the screen and the file always gets created but with 0 kb (empty).

Any other ideas?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
If you run the command in the Exchange Shell do you get any output on the shell .... if not some corrections need to be made in the command and not output

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

- Rancy
0
 
JesusSardinasAuthor Commented:
Yes, the data appears on the shell.  Even when I run the script with any sort of pipe it still spits out the information on the screen.  Even though it creates the file, no data is in the file.  That's the confusing part of this entire thing.  I'm sure it's something minor which I'm overlooking.
0
 
footechCommented:
Write-Host only writes to the console, not standard output, it can't be redirected.  You will need to use something like Write-Output instead, or perhaps use a Tee.
0
 
JesusSardinasAuthor Commented:
Footech could you please provide an example.  I tried both and neither worked.  Thanks.
0
 
JesusSardinasAuthor Commented:
Is there any other way to get the data piped into a file besides Write-Output?  That just spits everything out in one line as opposed to each item per column so it defeats the purpose.

Maybe the code has to be written in a different way?  Any ideas?
0
 
Exchange_GeekCommented:
May sound stupid but have you tried working with the script and apply the result in output file

Script.ps1 >filename.csv

OR

Script.pst1 | out-file FileName.csv

Regards,
Exchange_Geek
0
 
JesusSardinasAuthor Commented:
Yes I tried that and it didn't work.  Maybe the script needs to be written differently. The goal is to find all email forwards. The output must display the name of the email account being forwarded along with the name or email address where it's being forwarded.  The script I pasted works but it only displays the output on the screen because of the Write-Host command.

Is there a better way to write a script to get the output in a csv file?
0
 
Exchange_GeekCommented:
Why not use the script written by Bharat Suneja, convert it to .vbs and take an output just as I've written. Works as per him.

Regards,
Exchange_Geek
0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
Get-Mailbox -Filter {ForwardingAddress -ne $null} | foreach {$recipient = $_; $forwardingsmtp = (Get-Recipient $_.ForwardingAddress).DisplayName; Write-Output $recipient.Name, $forwardingsmtp, $recipient.DeliverToMailboxAndForward} > C:\abc.csv

- Rancy
0
 
footechCommented:
I think this may work using calculated properties.
Get-Mailbox -Filter {ForwardingAddress -ne $null} | foreach {$recipient = $_ | Select-Object Name, @{Name="ForwardingAddress"; Expression={ ((Get-Recipient $_.ForwardingAddress).DisplayName) }}, DeliverToMailboxAndForward } | Export-CSV file.csv

Open in new window

As a reference, using the following:
http://technet.microsoft.com/en-us/library/ff730948

Unfortunately, I can't test the actual commands as I don't have access to the right machines at this moment.
0
 
JesusSardinasAuthor Commented:
Thanks for the attempt footech.  That script didn't work.  It creates an empty file.  I tried both csv and txt files.
0
 
Exchange_GeekCommented:
@Jesus: You checked the vbs script on Bharat Suneja's site?

Regards,
Exchange_Geek
0
 
JesusSardinasAuthor Commented:
Rancy as I had mentioned before, that script works but it spits everything out in the same column instead of placing each piece of data in its respective column.  This apparently is a downside to using Write-Output.

Thanks for continuing to assist :)
0
 
JesusSardinasAuthor Commented:
Exchange_Geek I'm looking into that now.  Don't know how to convert from .ps1 to .vbs.  The original script I have been trying is from Bharat.  It only spits the output on the screen because it's using the Write-Host command.

http://exchangepedia.com/2005/10/list-users-with-email-forwarding-enabled.html
0
 
Exchange_GeekCommented:
VBS script is downloadable.

Regards,
Exchange_Geek
0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
What if we put a FT (format table) in between in place ...

- Rancy
0
 
JesusSardinasAuthor Commented:
Exchange Geek downloading it and changing to .ps1 doesn't work for me.  If I leave it as a .vbs file it does but prints data on the screen.  I tried piping to a file but it only creates an empty file.
0
 
Exchange_GeekCommented:
I'm afraid you'll need to run through a different script.

Regards,
Exchange_Geek
0
 
footechCommented:
Odd, I was about 95% sure that the code I provided would work.  I've used bits like on the link I provided and then piped them to export-csv with great results.  I must've made a mistake somewhere, but I won't be able to troubleshoot until Tuesday.  Hopefully you will have found the solution before then, but if not I'll give it a shot.

Good luck.
0
 
JesusSardinasAuthor Commented:
Rancy maybe I'm placing the FT in the wrong spot because it's still spitting out the data in one column.  Here's the code now:


Get-Mailbox -Filter {ForwardingAddress -ne $null} | foreach {$recipient = $_; $forwardingsmtp = (Get-Recipient $_.ForwardingAddress).DisplayName; Write-Output $recipient.Name, $forwardingsmtp,  $recipient.DeliverToMailboxAndForward | Format-List} > C:\abc.csv
0
 
JesusSardinasAuthor Commented:
Footech thanks!  I'm hoping as well but your support is greatly appreciated.
0
 
footechCommented:
OK, one last shot - I think I may have spotted my error.
Get-Mailbox -Filter {ForwardingAddress -ne $null} | foreach { $_ | Select-Object Name, @{Name="ForwardingAddress"; Expression={ ((Get-Recipient $_.ForwardingAddress).DisplayName) }}, DeliverToMailboxAndForward } | Export-CSV file.csv

Open in new window

0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
Format-List - this has to come before where specify the Output data for Example.

Get-Mailbox | FT Name, DN and etc

- Rancy
0
 
JesusSardinasAuthor Commented:
Footech your script worked but for some reason there are some rows which are left empty.  For example:

email1  forward1
email2  foward2
email3
email4 foward4

For whatever reason there's a few of those forwards which are not appearing at all but those emails ARE being forwarded.

Any idea why that may be?
0
 
JesusSardinasAuthor Commented:
Rancy the Format-List didn't work :(
0
 
footechCommented:
Have you verified that the .DisplayName property for these has any data?  Perhaps using .PrimarySmtpAddress instead is better?  I'm wildly guessing here, just an idea from a quick internet search.
0
 
JesusSardinasAuthor Commented:
Yes they are all properly populated.  It's a puzzle to me as well.
0
 
suriyaehnopCommented:
I think the cmdlet posted by footech already work for you. However there is row left empty. Maybe the reason is you already disable mail enable for "Forwarding" account but the account exist appear in AD. Thid happened to me as well.

Below is my code:

Get-Mailbox -ResultSize Unlimited | where {$_.ForwardingAddress -ne $null} | Format-Table name,primarysmtpaddress,@{Label='Forward';expression={ (Get-Recipient -Identity $_.ForwardingAddress).primarysmtpaddress }}

Open in new window

0
 
JesusSardinasAuthor Commented:
Thanks all for your assistance!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 15
  • 6
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now