Easy powershell script
Hi all, I am stumbling having spent 20 mins with a brain freeze on how to get my results to export to a txt file or CSV file!! I have compiled a 3 line script to get-mailboxes and return 3 attributes....Account name, Database they are on, and todays date. I created a function for "date". BUT, I just cant figure out how to export it to a txt file or csv file. Here's the script: $allmbx=get-mailbox $today=get-date foreach ($mbx in $allmbx) {write-host $mbx.samaccountname,$mbx.database,$today} I'm going to be calling it from a Scheduled TASK and I tried it by appending | export-csv c:\test.csv to the bottom of the script for instance, but it just wont work and I cant remember how to do it.....falling at the last hurdle. (It keeps referring to an "empty pipe line). Can someone point out where I'm going wrong please? M
March 1st, 2011 1:24pm

Hi there, I'm no scripting GURU either, but try this: $today=get-date Get-Mailbox | foreach { $one=$_.DisplayName $two=$_.database "$one $two $today" } | out-file c:\scripts\test.txt It's easier and would allow you to pipe through to export it. edit: the variables that I'm using like $one $two are just for illustration. You can name them whatever you'd like.
Free Windows Admin Tool Kit Click here and download it now
March 1st, 2011 3:21pm

You can try the below lines. $today = get-date Get-Mailbox -resultsize unlimited | select displayname, Database, @{name = "Date"; expression = {$today.tostring()}} Note: You can even modify get-date to get the date in any format you wish to.
March 2nd, 2011 6:43am

Oops...missed out the last piece. $today = get-date Get-Mailbox -resultsize unlimited | select displayname, Database, @{name = "Date"; expression = {$today.tostring()}} | export-csv <filename.csv> To append the data to a single file, you use $today = get-date Get-Mailbox -resultsize unlimited | select displayname, Database, @{name = "Date"; expression = {$today.tostring()}} | add-content filename.txt
Free Windows Admin Tool Kit Click here and download it now
March 2nd, 2011 6:46am

Hi Millardus, I have done a local test, and the suggestion given by Rajitha will do help you, just modify the filed "DisplayName" to "SamAccountName". Best regards, Serena Please remember to click Mark as Answer on the post that helps you, and to click Unmark as Answer if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
March 3rd, 2011 2:40am

Hi folks, sorry for not getting back to you all sooner (nature of IT, working on 1 thing and then something completely different drags you away!!) I went with the following EMS commands into a ps file in the end which, in combination with a pure powershell script, (Which renames the output file, moves it to another server and then renames the source file again prefixing with "copied") has done the job nicely. $today=get-date get-mailbox -resultsize unlimited | select-object alias, displayname, Database, {$today} | export-csv -notypeinformation Thanks for all the input. Tom
Free Windows Admin Tool Kit Click here and download it now
March 15th, 2011 12:45pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics