Avatar of jack jones
jack jones
 asked on

not able to export result in csv - powershell

$a = Get-Content C:\xyz\test.txt
foreach($b in $a)
{
Get-Mailbox -Identity $b | Select-Object name, Database
}
$b | Export-Csv C:\xyz\result.csv 

Open in new window


It gives output as ""PSPath","PSParentPath","PSChildName","PSDrive","PSProvider","ReadCount","Length.. and so on"
Powershell* ExportExchange

Avatar of undefined
Last Comment
jack jones

8/22/2022 - Mon
Chris Dent

You're exporting b, which comes from your file. It's breaking the interpreter a bit.
Get-Content C:\xyz\test.txt | ForEach-Object {
    Get-Mailbox -Identity $_ | Select-Object name, Database
} | Export-Csv C:\xyz\result.csv 

Open in new window

jack jones

ASKER
Supply values for the following parameters:
Process[0]:
Anyway you could modify my script not able to get yours..
Chris Dent

$results = @()
$a = Get-Content C:\xyz\test.txt
foreach($b in $a)
{
    $results += Get-Mailbox -Identity $b | Select-Object name, Database
}
$results | Export-Csv C:\xyz\result.csv

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
jack jones

ASKER
now the script is giving error operation could not be performed
Chris Dent

You're going to have to share a bit more detail because the errors you're getting and the code I'm posting are not joining up.

What, exactly, does the error message say and where is it being thrown?
jack jones

ASKER
what my script does is it gets all user from list and find their mailboxes and shows results.
I want this result in csv
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chris Dent

I'm quite capable of reading the script and determining its intent. I've given you two versions which will achieve that result. The scripts aren't long, there's not a huge margin for error here.

I'm unclear how you're managing to end up with errors. You'll have to share more if I'm to help further.
jack jones

ASKER
$a = Get-Content C:\xyz\test.txt
foreach($b in $a)
{
Get-Mailbox -Identity $b | ft name, Database
}
$b | Export-Csv C:\xyz\result.csv 

Open in new window


I have modified my script it gives output in the console however it generates a csv file with no output in it
ASKER CERTIFIED SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jack jones

ASKER
Im getting this ou put in csv
#TYPE Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
"ClassId2eaqwere99d3c952918aff9cd","pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry"

May be some with exchange expertise can solve this ...
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Chris Dent

You've added format-list or format-table...
jack jones

ASKER
format-table ft ... i tried both but none succeed
Chris Dent

That's why its broken now. You cannot send output created by the format commands to export-csv.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jack jones

ASKER
Yes thats what i m looking to solve.
Chris Dent

You showed code with a bug caused by you passing the wrong value to Export-Csv.

I gave you two solutions which deal with that.

At no point have I used Format-*, so why are you?

I cannot help you, you change the things you're given, then complain they don't work.

Good luck.
jack jones

ASKER
Both didnt work :(
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Jeff Glover

I hate to ask this but are you running this on an Exchange server or at least opening a  remote powershell session to one? I.E. loading the Exchange modules? I only ask this because, out of curiosity, I tried both of the scripts that Chris posted and they run like a champ ). As long as your text file is just  a list of users, you have valid paths, and have the Exchange module loaded in powershell these run perfectly.
jack jones

ASKER
Yes i have loaded an exchange module in powershell ..