none
Export variable to CSV RRS feed

  • Question

  • Looking to export data I have collected in variables to a CSV file. So far this is what I have...

    $UserList = @()
    $Users = (Get-ADGroup $Group -Properties Members).members | Get-ADUser -Properties SamAccountName | select -ExpandProperty SamAccountName
    
    foreach ($User in $Users)
    {
          $Result = Get-Mailbox $User | select -ExpandProperty PrimarySmtpAddress
          $UserList += $Result
    }
    
    [PSCustomObject]@{
    'LAN-ID' = $Users
    'Email Address' = $UserList
    } | Export-Csv c:\CSVResult.csv -NoTypeInformation   
    

    This yields output with "System.Object[]" in the CSV file because it is being cast as a string when using Export-CSV. I'm struggling trying to find a way to perform this task though. Any help is appreciated.

    • Moved by Bill_Stewart Wednesday, September 4, 2019 6:28 PM This is not "fix/debug/rewrite script for me" forum
    Friday, March 8, 2019 3:18 PM

All replies

  • Get-ADGroupMember $Group
        ForEach-Object{
            Get-Mailbox $User.SamAccountName
        } |
        Select Alias, PrimaryEmailAddress |
        Export-Csv c:\CSVResult.csv -NoTypeInformation  
    It would help if you took some time to learn PowerShell instead of guessing.

    \_(ツ)_/


    • Proposed as answer by Imran Aftab Friday, March 8, 2019 5:30 PM
    • Unproposed as answer by Imran Aftab Friday, March 8, 2019 5:47 PM
    • Edited by jrv Saturday, March 9, 2019 9:16 AM
    Friday, March 8, 2019 5:22 PM
  • Or maybe I have taken the time to learn, and I am just stuck on this part... Ever think that something else may be the case other than your confrontational idea first? I clearly stated I understood why my code wasn't working, did that not tip you off that.. "Hey, maybe this guy did do some research"? Obviously not. Next time refrain from commenting please, I would rather read nothing than a post like this. It would be different if I asked someone to hold my hand. Did I master 100% of the PowerShell language before making this post? No, I didn't, but it's not binary, it's possible that I have taken time to learn something and am just stuck on a certain topic.

    Get-ADGroupMember doesn't work for me here anyway because I am hitting it's limit of 5,000, which is why I clearly typed Get-ADGroup in my code, since that is what I'm using.

    • Edited by RK-33 Friday, March 8, 2019 5:41 PM
    Friday, March 8, 2019 5:37 PM
  • Agree. No one is perfect, we learn everyday and if we get stuck at some point then we seek someone's help. We should all try to be nice on forum as much as possible. 
    Friday, March 8, 2019 5:47 PM
  • If you learned then you learned the wrong things.  What book or tutorial did you use or did you just copy what you have seen on the Internet.

    You cannot learn programming by copying things found on the Internet.  Blogs and articles are only useful when you fully understand the basics.

    If you look carefully at your code you would see that the mistakes shows you do not understand the basics at all.

    I am not picking on you.  I just want you to understand that you have either a very bad idea of what PowerShell and coding are or you have not taken any time to learn how to use basic syntaxes like a "foreach" loop. This will make everything you do going forward much more difficult.


    \_(ツ)_/

    Friday, March 8, 2019 5:48 PM
  • Aside from that, thank you for your response. I was able to figure it out on my own though after a little more trial and error. For those of you who also may run into something like this, here is what I did...

    $Table = @()
    $Users = (Get-ADGroup $Group -Properties Members).members | Get-ADUser -Properties SamAccountName
    
    foreach ($User in $Users)
    {
        $Result = Get-Mailbox $User.samaccountname
        $UserList += $Result
    }
    
    foreach ($Item in $UserList)
    {
        $Table += New-Object PsObject -Property @{
        'LAN-ID' = $Item.samaccountname
        'Email Address' = $Item.primarysmtpaddress
        }
    }
    
    $Table | Export-Csv c:\CSVResults.csv -NoTypeInformation
    

    Friday, March 8, 2019 5:48 PM
  • Be that as it may, if I learned the hard way, or if I had to learn the wrong things first to get to the right things, then that is they way I did it. It still doesn't mean I didn't "learn" anything. I don't have to do it the same way you did by any means. Either way, thanks again for taking the time to respond. Your efforts are appreciated.
    • Edited by RK-33 Friday, March 8, 2019 5:50 PM
    Friday, March 8, 2019 5:50 PM
  • Aside from that, thank you for your response. I was able to figure it out on my own though after a little more trial and error. For those of you who also may run into something like this, here is what I did...

    $Table = @()
    $Users = (Get-ADGroup $Group -Properties Members).members | Get-ADUser -Properties SamAccountName
    
    foreach ($User in $Users)
    {
        $Result = Get-Mailbox $User.samaccountname
        $UserList += $Result
    }
    
    foreach ($Item in $UserList)
    {
        $Table += New-Object PsObject -Property @{
        'LAN-ID' = $Item.samaccountname
        'Email Address' = $Item.primarysmtpaddress
        }
    }
    
    $Table | Export-Csv c:\CSVResults.csv -NoTypeInformation


    This code is also wrong and shows a lack of understanding of how PowerShell works.  You are writing 5 times as much code as necessary.  Please take the time to do a tutorial or get one of the many books for new PowerShell users.

    \_(ツ)_/

    Friday, March 8, 2019 5:57 PM
  • Be that as it may, if I learned the hard way, or if I had to learn the wrong things first to get to the right things, then that is they way I did it. It still doesn't mean I didn't "learn" anything. I don't have to do it the same way you did by any means. Either way, thanks again for taking the time to respond. Your efforts are appreciated.

    With technology, science, math and engineering you would have to reinvent the wheel to "figure it out".  Along the way you have to make many mistakes and you will learn things incorrectly.  Take my word for it.  I have seen this for decades.  If you insist on taking the slow boat to nowhere then enjoy your self.  You may learn a few things but you will always be getting stuck. My suggestion is only intended to help you learn correctly and faster which will ultimately make PowerShell more fun and more useful.

    \_(ツ)_/

    Friday, March 8, 2019 6:01 PM
  • Agree. No one is perfect, we learn everyday and if we get stuck at some point then we seek someone's help. We should all try to be nice on forum as much as possible. 

    To try learn any advanced technology by only asking others is both foolish and naïve.  All successful technicians always start by taking the time to learn the basics of the technology using some formal method.  In the past we only had the documentation and vendor training.  Today we have video tutorials and well written books as well as vendor documentation published on the web.

    What I see now is that most new scripters and techs coming into these technical forums have no training,  do not know how to use the documentation and have not learned how to access free training material.  It seems that most do not know how to read books.  Apparently Twitter and forum posts are the limit of the reading skills of most newer techs.

    I am only recommending that those wanting to program with PowerShell, as opposed to suing it as a command shell, have no basic programming knowledge and seem too think they only need to guess.

    Learn the basics then asking others is more productive. Without a clear understanding of the technologies in use the questions will be poorly asked and the answers will not be understood.


    \_(ツ)_/

    Friday, March 8, 2019 6:08 PM
  • Be that as it may, if I learned the hard way, or if I had to learn the wrong things first to get to the right things, then that is they way I did it. It still doesn't mean I didn't "learn" anything. I don't have to do it the same way you did by any means. Either way, thanks again for taking the time to respond. Your efforts are appreciated.

    I suppose you would make the same naïve claim if you were learning to be a brain surgeon.  Try to get a pilots license by refusing to study aviation, aeronautics or navigation.

    Do you think technology is so trivial that someone with no technical training can just guess at how it works.   Do you think this was built by people who guessed at things. 

    I find that many people who want to be technicians today do not even know how a computer works and is able to turn text typed at  prompt into functionality.  How to ones and zeros and a simple transistor switch do that?  Do you know?  Can you describe the results of 100+ years of engineering that gave us the stored program computer. I doubt it.


    \_(ツ)_/

    Friday, March 8, 2019 6:13 PM
  • There you go again, it's not "wrong". It may not be how "you" decide to write it, but it certainly doesn't make it wrong. If I added more lines of code than you would have, then so be it. The world doesn't revolve around "your" method to writing code. The above code works, and runs well too. Me writing it with a few more lines than you is rhetorical.

    Friday, March 8, 2019 6:24 PM
  • Your example is apples to oranges, your comparing brain surgery and learning a coding language. There are no lives at stake when learning PowerShell, although you certainly make it seem that way. All code is, and can be, "tested" before put into production, as I practice. You cannot test brain surgery on a human first, big difference and a very unfair analogy. Do you know how to perform brain surgery? Do you know the in's and out's of human anatomy? I doubt that as well. It's a two way street with everything you are mentioning, not a highway and a bike path. Bottom line, you are not god and you are criticizing my code as "wrong" (your words) because you can write it shorter. Run times will be roughly if not exactly the same between what I have above and anything you can write.

    You continue to accuse me of guessing as well, where you are wrong again. Once more, it's not binary, just because you interpret my example code a certain way you are making claims that I am guessing, when there is so much more in between, it's not "your either right or your guessing". 

    Friday, March 8, 2019 6:29 PM
  • Aside from that, thank you for your response. I was able to figure it out on my own though after a little more trial and error. For those of you who also may run into something like this, here is what I did...

    $Table = @()
    $Users = (Get-ADGroup $Group -Properties Members).members | Get-ADUser -Properties SamAccountName
    
    foreach ($User in $Users)
    {
        $Result = Get-Mailbox $User.samaccountname
        $UserList += $Result
    }
    
    foreach ($Item in $UserList)
    {
        $Table += New-Object PsObject -Property @{
        'LAN-ID' = $Item.samaccountname
        'Email Address' = $Item.primarysmtpaddress
        }
    }
    
    $Table | Export-Csv c:\CSVResults.csv -NoTypeInformation

    Here is a fist level reduction of your guesses.

    $Users = (Get-ADGroup $Group -Properties Members).members | Get-ADUser -Properties SamAccountName
    $table = foreach ($User in $Users) {
        [pscustomobject]@{
            'LAN-ID' = $User
            'Email Address' = (Get-Mailbox $User).PrimaryEmailAddress
        }
    }
    $table | Export-Csv c:\CSVResults.csv -NoTypeInformation

    If you had learned the basics this would be obvious.  It is pretty much PowerShell 101.

    Again, this and yours are incomplete and also naïve because the wrong CmdLets are being used for the task.  They might work but it is not the correct path to a solution because the better CmdLets should be used.

    This deficiency is caused by a lack of understanding of how to choose the code needed for a solution.  Learning the basics will show you what steps to take to learn about a modules CmdLets and how to discover for yourself the best way to use them. What you do not understand about PowerShell you will not learn from copying code in a forum.  You way of beginning and learning will cause you to ignore examples that are better and that use PowerShell correctly.  Again I say you cannot do brain surgery without training in basic biology.


    \_(ツ)_/


    • Edited by jrv Friday, March 8, 2019 6:41 PM
    Friday, March 8, 2019 6:36 PM
  • Last comment.  One of the biggest causes of corporate security breaches is naïve techs who have not learned the technology and incorrectly assume that what they do is not critical.

    Don't trivialize the profession you are trying to join.  Technicians are critical and need to consider life and death consequences.  A security breach may lead to loss of life many industries. At minimum it can cost a company millions of dollars to remedy.  Naïve and stubborn techs are one of the most common causes or security breaches although  all employees can contribute.  Incorrect security settings and procedures that allow scripts to access and change things are dangerous if written by untrained techs.

    In just s simple practical sense you are wasting a huge amount of your time and you companies time by not taking a professional approach to this.  You would never get away with it in most companies and definitely not in the military.  Why be unprofessional when you can be a super-tech?


    \_(ツ)_/

    Friday, March 8, 2019 6:47 PM
  • Get-ADGroupMember $Group
        ForEach-Object{
            Get-Mailbox $User.SamAccountName
        } |
        Select Alias, PrimaryEmailAddress
        Export-Csv c:\CSVResult.csv -NoTypeInformation  
    It would help if you took some time to learn PowerShell instead of guessing.

    \_(ツ)_/

    Shouldn't there be pipe into 'Export-Csv'?

    ...
        Select Alias, PrimaryEmailAddress |
        Export-Csv c:\CSVResult.csv -NoTypeInformation

    Keith


    Keith

    Saturday, March 9, 2019 4:19 AM
  • Shouldn't there be pipe into 'Export-Csv'?

    ...
        Select Alias, PrimaryEmailAddress |
        Export-Csv c:\CSVResult.csv -NoTypeInformation

    Keith


    Keith

    Yes.  Thank you.  It is fixed.

    \_(ツ)_/

    Saturday, March 9, 2019 9:17 AM