locked
Save .CSV File As $_. Value RRS feed

  • Question

  • Hi All,

    I have a CSV file with a list of users (see code below)


    import-csv .\Musers.csv | foreach {get-mailbox $_.name | Select Name, @{Name=’EmailAddresses’;Expression={[string]::join(";", ($_.EmailAddresses))}} | Export-CSV $_.name.csv }

    I need to export each user data to a CSV file with the name of the user In the CSV file (name).

    The file should be named the same as the value In the current variable $_.name

    Can you help?

    Many thanks,

    Simon



    • Edited by Simon201 Monday, July 3, 2017 2:04 AM
    Monday, July 3, 2017 1:37 AM

Answers

  • At first, it is not handy to put everything in one single line. This is quite confusing and difficult to debug.

    But to your question. This should work:

    Export-Csv "$($_.name).csv"

    • Marked as answer by Simon201 Monday, July 3, 2017 10:30 PM
    Monday, July 3, 2017 9:11 AM
  • Foreach and the pipe don't always play well with the $_, try using a variable to store the info then export it.

    Could look similar to this,

    Import-Csv .\Musers.csv | foreach {
        $Variable = get-mailbox $_.name | Select Name, @{Name='EmailAddresses';Expression={[string]::join(";", ($_.EmailAddresses))}} 
        Export-CSV "$($Variable.name).csv"
        }

    Cheers!


    Please remember to mark the replies as answers if they help.

    • Marked as answer by Simon201 Monday, July 3, 2017 10:30 PM
    Monday, July 3, 2017 2:12 PM
  • To make it beautiful, ypu can spilt it up even more:
    $MailData = import-csv .\Musers.csv 
    
    foreach($m in $MailData) 
    {
        $MailAdress = Get-Mailbox $m.Name | Select Name, @{ Name='EmailAddresses';Expression={[string]::join(";", ($m.EmailAddresses))} }
        $FileName = $m.Name
        $MailAdress | Export-CSV "$FileName.csv" -Delimiter "," -NoTypeInformation 
    }
    Always remember, that the ones who follow you must be able to understand your code. And, first of all, the code must be understandable in 2 or 3 years by yourself.

    • Edited by hpotsirhc Tuesday, July 4, 2017 5:50 AM
    • Marked as answer by Simon201 Tuesday, July 4, 2017 6:48 AM
    Tuesday, July 4, 2017 5:49 AM

All replies

  • Please fix your post.  It is broken and in readable.


    \_(ツ)_/


    • Edited by jrv Monday, July 3, 2017 1:39 AM
    Monday, July 3, 2017 1:38 AM
  • Should be OK now.
    Monday, July 3, 2017 2:05 AM
  • At first, it is not handy to put everything in one single line. This is quite confusing and difficult to debug.

    But to your question. This should work:

    Export-Csv "$($_.name).csv"

    • Marked as answer by Simon201 Monday, July 3, 2017 10:30 PM
    Monday, July 3, 2017 9:11 AM
  • Foreach and the pipe don't always play well with the $_, try using a variable to store the info then export it.

    Could look similar to this,

    Import-Csv .\Musers.csv | foreach {
        $Variable = get-mailbox $_.name | Select Name, @{Name='EmailAddresses';Expression={[string]::join(";", ($_.EmailAddresses))}} 
        Export-CSV "$($Variable.name).csv"
        }

    Cheers!


    Please remember to mark the replies as answers if they help.

    • Marked as answer by Simon201 Monday, July 3, 2017 10:30 PM
    Monday, July 3, 2017 2:12 PM
  • Many thanks, It works !!!!
    Monday, July 3, 2017 10:30 PM
  • To make it beautiful, ypu can spilt it up even more:
    $MailData = import-csv .\Musers.csv 
    
    foreach($m in $MailData) 
    {
        $MailAdress = Get-Mailbox $m.Name | Select Name, @{ Name='EmailAddresses';Expression={[string]::join(";", ($m.EmailAddresses))} }
        $FileName = $m.Name
        $MailAdress | Export-CSV "$FileName.csv" -Delimiter "," -NoTypeInformation 
    }
    Always remember, that the ones who follow you must be able to understand your code. And, first of all, the code must be understandable in 2 or 3 years by yourself.

    • Edited by hpotsirhc Tuesday, July 4, 2017 5:50 AM
    • Marked as answer by Simon201 Tuesday, July 4, 2017 6:48 AM
    Tuesday, July 4, 2017 5:49 AM
  • There's a join operator in powershell already:

    $_.EmailAddresses -join ';' 
    

    Tuesday, July 4, 2017 1:13 PM