none
Adding Objects from different Get-x command to a new csv file with columns and headers for each. RRS feed

  • Question

  • Very new to PowerShell and need some help.

    I would like to be able to, for example 

    (get-netadapter).name #and add the output to a csv file in a single column with the header Nic-Name.

    Then

    (get-psdrive).name and add it to the same csv file in a second column with the column header Drive-Name.

    All I really need is one example and I am off to the races....

    Did I mention I was new to PowerShell.


    tink5150

    • Moved by Bill_Stewart Friday, March 9, 2018 7:21 PM User doesn't understand scope of own question
    Sunday, July 30, 2017 9:13 AM

All replies

  • Because in a normal Windows installation you usually find more than one netadapter and more than one PSDrive you would have to "connect" the outputs of these two cmdlets together to fit each in one column. But anyway that's of course possible if you really want to:
    [PSCustomObject]@{
    	NetAdapters = (get-netadapter).name -join ';'
    	PSDrives = (get-psdrive).name  -join ';'
    } | Export-Csv -Path C:\sample\output.csv -Delimiter ',' -NoTypeInformation
    
    Custom object can help you in a lot of situations. Here you can find some more information about it:  https://technet.microsoft.com/en-us/library/ff730946.aspx

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by jrv Sunday, July 30, 2017 6:41 PM
    Sunday, July 30, 2017 10:09 AM
  • That is good to know but is not what I am looking for.

    I do not want to join the two commands to be included in a single column.

    I would like the output to look like the following

    Nic-Name Drive-Name
    vEthernet (lemmout) Alias
    vEthernet (HNS Internal NIC) C
    Ethernet 2 Cert
    Ethernet D
    Ethernet 3 E
    Env
    F
    Function
    HKCU
    HKLM
    Variable
    WSMan


    tink5150

    Sunday, July 30, 2017 10:21 AM
  • Hmmm .... that's not the way a CSV file or the CSV format works. And it does not make sense actually this way. What would you use this format for? CSV is made to store data in a reusable format.

    You might rethink what you want to achieve. This way you will have a lot of work and IMHO no benefit at all from it.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by jrv Sunday, July 30, 2017 6:50 PM
    Sunday, July 30, 2017 12:03 PM
  • Your last post was deleted.  It had something in it that caused the whole thread to fail. I suspect the site manager saw what it was.  If you deleted your own post then ignore this as the thread is not working correctly again.

    Be careful posting formatted text or code as it can break the thread.


    \_(ツ)_/


    • Edited by jrv Sunday, July 30, 2017 6:50 PM
    Sunday, July 30, 2017 6:48 PM
  • I deleted it because it was not an appropriate response, having said that just because it does not make sense to you doesn't mean it does not make sense.

    If like me you are unable to format the csv table for data collection as I have pointed out then please do not say it does not make sense just say I can't do it or don't respond.


    tink5150

    Sunday, July 30, 2017 7:03 PM
  • Food for thought:

    There is no built in way in PowerShell to do what you ask.  You can write a script that will create that format. of an output but you need to write it.  We cannot write it for you.

    You need to find a ways to loop through both collections and output the format you want.  Search and you will find examples of many ways to create custom output formats.

    "New to PowerShell" usually means you have not read any of the many books or taken any of the tutorials that teach you how to use PowerShell. If you have no programming training then you must do the training to get started. These forums are not for teaching you the basics.

    Yes.  What you ask makes no technical sense even if it is what you want.  A CSV is rows of related data.  It is an information vehicly used to maintain the relationship between data in a row.  It is not just a simple two column display mechanism.

    To understand a CSV read the following: https://en.wikipedia.org/wiki/Comma-separated_values


    \_(ツ)_/

    Sunday, July 30, 2017 7:05 PM
  • I just do not want to leave this without a comment. 

    In general - if you ask people for help you will get a lot of opinions - especially in internet forums even if it's a more technical environment like Powershell.  I used to think that hopefilly about 99% of the people just want to help you. Usually they don't get paid for to help you.  (I even pointed out my answer as opinion with the IMHO)

    In your special case - really often we see some questions especially from novices where the question is actually asked wrong. Let me use an analogy to illustrate what I mean. If you ask an engineer how to make a car go sideways because you want to have the opportunity to carry stuff not only straight ahaed. Of course you could counstruct a car with an additional set of wheels mounted 90 degrees to the original ones. But honestly ... that would be really rediculous. So the engineer would recommend to make the front wheels steerable. Because that's the tried, tested, proven and most proffessional method to achieve maneuverability. 

    Have a lot of fun! ;-)


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Sunday, July 30, 2017 8:08 PM
  • Unfortunately this engineer did not read your post and buit a sideways car anyway: https://www.youtube.com/watch?v=vDAU5n4oBZY


    \_(ツ)_/

    Sunday, July 30, 2017 8:23 PM
  • Or one with 4 wheel steering.  Quadrasteer from General Motors, while not quite sideways driving improved steering performance. https://www.bing.com/videos/search?q=Quadrasteer+&&view=detail&mid=C40AFE860331AF762028C40AFE860331AF762028&FORM=VRDGAR

    Thanks for the responses guys.


    tink5150

    Sunday, July 30, 2017 8:40 PM
  • So the end result is that you can invent anything you want but you have to do your own engineering.  We can answer questions about scripting and help correct mistakes but the forum is not for custom designs. 

    In 30 years I have never seen anyone ask for a format like you are asking for.  I don't think you will find any examples of that exact format.  Like the guy who built the sideways car you have to do you own design and engineering.  If you can learn how to do that you will end up much better understanding of PowerShell.


    \_(ツ)_/

    Sunday, July 30, 2017 8:46 PM
  • I don't disagree with the engineering statement and believe me what I asked for was to help fill a knowledge gap, not have someone write the entire script I have in mind for me.

    I bet in your 30 Years you have seen some pretty big excel spreadsheet yes, chock full of data that seems unrelated when looking at the individual pieces, but when combined does provide useful info?  I know I have seen those in my 30 years.


    tink5150

    Sunday, July 30, 2017 8:53 PM
  • I have spent a very large amount of time helping companies like AT&T, PSE&G and others get rid of all of the very bad spreadsheets.  Just because you can build a sheet like that does not make it useful outside of being a jumble of data that is unmanageable.  That is usually caused by untrained people using Excel like a word processor.

    Of course a spreadsheet can have named data sections anywhere.  These named ranges can be used to feed reports but, believe me, in these cases the data is organized and related.  It is not intended that you look at all pieces. The pieces add up to some kind of report.  Yes you can do your requirement in Excel very easily as Excel has no rules for what you can enter.  You can even use PowerShell to input the columns one at a time into Excel.  Look in the Gallery for examples.


    \_(ツ)_/

    Sunday, July 30, 2017 9:01 PM
  • LOL ... James, I should have known before that you gonna kick my ass. Thanks for almost ruin my argument!   :-P  ;-)  :-D

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Sunday, July 30, 2017 9:49 PM
  • LOL ... James, I should have known before that you gonna kick my ass. Thanks for almost ruin my argument!   :-P  ;-)  :-D

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    I couldn't resist.  It was just hanging out there waiting.  Never trust an engineer.

    \_(ツ)_/

    Sunday, July 30, 2017 9:51 PM