locked
Not getting output in proper CSV/text when using cmdlet Export-CSV or Convertto-CSV | Out-File. RRS feed

  • Question

  • May be I am missing some basics, here is a small example.

    Get-DHCPServerInDC | FL -Property DNSName | Export-CSV -Path DHCPServer.CSV 

    And I am getting some unformatted output.

    How to get / convert this to a formatted string or text?

    Thanks in advance.

    Roushan

    Wednesday, September 14, 2016 8:05 AM

Answers

  • Hi Andy, Just figured out to resolve this issue, and below is the simple script which worked for me when i used FL or FT and getting an output to a proper CSV :)

    # This script will allow to export information to CSV even using FL and FT
    Get-WmiObject -Class Win32_BIOS | FL Manufacturer,Name,Version | Out-File -FilePath '.\1.txt' -Encoding default
    $Var1 = Get-Content -Path '.\1.txt'
    $Var1 -replace ":","," | Out-File -FilePath .\FinalOutput.csv -Encoding default

    I have tested the same logic for a long script output and it worked.

    Thanks

    Roushan


    • Edited by Roushan_K Thursday, September 22, 2016 6:05 AM
    • Marked as answer by Roushan_K Thursday, September 22, 2016 6:05 AM
    Thursday, September 22, 2016 5:58 AM

All replies

  • Get-DHCPServerInDC | select -Property DNSName | Export-CSV -Path DHCPServer.CSV -NoTypeInformation


    Gleb.

    • Proposed as answer by Hello_2018 Thursday, September 15, 2016 3:20 AM
    Wednesday, September 14, 2016 8:10 AM
  • Many thanks Gleb, it worked. Just curious to know FL or FT not works if we use Export-CSV?

     
    Wednesday, September 14, 2016 8:18 AM
  • Hi Roushan_K,

    >>Just curious to know FL or FT not works if we use Export-CSV?

    If you need to format the output file, you could use out-file rather than export-csv.

    Get-DHCPServerInDC | FL -Property DNSName | out-file -Path DHCPServer.CSV 

    Based on my understanding, it was caused due to the different output mechanism.

    If you used :Get-DHCPServerInDC | FL -Property DNSName ,you'll get this type format: ’TypeName: Microsoft.PowerShell.Commands.Internal.Format.FormatEndData‘ while 'Get-DHCPServerInDC | select -Property DNSName ' get this type:Selected.Microsoft.Management.Infrastructure.CimInstance.

    For deep research, you could refer to MSDN related links:

    https://msdn.microsoft.com/en-us/library/microsoft.management.infrastructure.ciminstance(v=vs.85).aspx

    Best regards,

    Andy_Pan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    • Edited by Hello_2018 Thursday, September 15, 2016 3:21 AM
    Thursday, September 15, 2016 3:20 AM
  • Thanks Andy, formatting is needed many times before exporting a output in a file format. 

    i will explore more to get some work around.

    Regards

    RK


    • Edited by Roushan_K Thursday, September 15, 2016 8:18 AM
    Thursday, September 15, 2016 8:18 AM
  • Hi Roushan_K,

    You're welcome.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Andy_Pan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, September 20, 2016 6:53 AM
  • What I have got till now is below script, which will at least give some output when we use FL or FT which is not pretty much properly formatted.

    Get-WmiObject -Class Win32_Service | FT Name,StartName,StartMode > C:\Temp\1.csv

    Any further advise will be appreciated.

    Thanks

    Roushan

    Wednesday, September 21, 2016 9:23 AM
  • Use Select-Object instead of Format-Table and then pipe into Export-Csv.

    Wednesday, September 21, 2016 3:36 PM
  • Hi Andy, Just figured out to resolve this issue, and below is the simple script which worked for me when i used FL or FT and getting an output to a proper CSV :)

    # This script will allow to export information to CSV even using FL and FT
    Get-WmiObject -Class Win32_BIOS | FL Manufacturer,Name,Version | Out-File -FilePath '.\1.txt' -Encoding default
    $Var1 = Get-Content -Path '.\1.txt'
    $Var1 -replace ":","," | Out-File -FilePath .\FinalOutput.csv -Encoding default

    I have tested the same logic for a long script output and it worked.

    Thanks

    Roushan


    • Edited by Roushan_K Thursday, September 22, 2016 6:05 AM
    • Marked as answer by Roushan_K Thursday, September 22, 2016 6:05 AM
    Thursday, September 22, 2016 5:58 AM
  • You're welcome, Roushan_K.

    Good to hear that you have solved this issue by yourself. In addition, thanks for sharing your solution in the forum as it would be helpful to anyone who encounters similar issues.

    If there is anything else we can do for you, please feel free to post in the forum.

    Have a nice day!

    Best regards,

    Andy_Pan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, September 22, 2016 6:43 AM
  • Out of curiosity - why do you insist on using Format-* cmdlets when PowerShell can easily export to a CSV already?


    Thursday, September 22, 2016 1:03 PM
  • Hi Mike,

    I have a script for infrastructure consolidated lengthy report which gives me output in vertical by default and I need the same in horizontal so used FL and it gives me ugly output. It mean when we need to change format of output instead of default Powershell does not help directly. Small eg below.

    Gsv -name vm* | FL -Property Name,Status | Export-csv -path C:\1.csv 

    Your advise really appreciated.

    Thanks

    Roushan

    Thursday, September 22, 2016 4:16 PM