Power Shell CSV conversion












0















Hi I am trying to export a text file to csv format in Powershell but facing some alignment issue.data not coming properly.



Input text



clexec.py "top -d1|grep Swap"
NVA-0055-Prod-N08:Swap: 48G Total, 1428K Used, 48G Free
NVA-0055-Prod-N01:Swap: 48G Total, 332K Used, 48G Free
NVA-0055-Prod-N04:Swap: 48G Total, 169M Used, 48G Free
NVA-0055-Prod-N05:Swap: 48G Total, 884K Used, 48G Free
NVA-0055-Prod-N02:Swap: 48G Total, 4236K Used, 48G Free
NVA-0055-Prod-N06:Swap: 48G Total, 132K Used, 48G Free
NVA-0055-Prod-N07:Swap: 48G Total, 92K Used, 48G Free
NVA-0055-Prod-N03:Swap: 48G Total, 712K Used, 48G Free


I am using below below command



$a= import-csv 'C:UsersPDC_AVERE_SWAP_OP.txt' -Delimiter 't' -Header 'Name','Total','used','free'
$a | export-csv csvfile.csv -NoTypeInformation


My output coming like below



enter image description here



after correcting th delimiter part I am getting all the data in a single column but I need it to split among the columns Total,used and free



enter image description here










share|improve this question





























    0















    Hi I am trying to export a text file to csv format in Powershell but facing some alignment issue.data not coming properly.



    Input text



    clexec.py "top -d1|grep Swap"
    NVA-0055-Prod-N08:Swap: 48G Total, 1428K Used, 48G Free
    NVA-0055-Prod-N01:Swap: 48G Total, 332K Used, 48G Free
    NVA-0055-Prod-N04:Swap: 48G Total, 169M Used, 48G Free
    NVA-0055-Prod-N05:Swap: 48G Total, 884K Used, 48G Free
    NVA-0055-Prod-N02:Swap: 48G Total, 4236K Used, 48G Free
    NVA-0055-Prod-N06:Swap: 48G Total, 132K Used, 48G Free
    NVA-0055-Prod-N07:Swap: 48G Total, 92K Used, 48G Free
    NVA-0055-Prod-N03:Swap: 48G Total, 712K Used, 48G Free


    I am using below below command



    $a= import-csv 'C:UsersPDC_AVERE_SWAP_OP.txt' -Delimiter 't' -Header 'Name','Total','used','free'
    $a | export-csv csvfile.csv -NoTypeInformation


    My output coming like below



    enter image description here



    after correcting th delimiter part I am getting all the data in a single column but I need it to split among the columns Total,used and free



    enter image description here










    share|improve this question



























      0












      0








      0








      Hi I am trying to export a text file to csv format in Powershell but facing some alignment issue.data not coming properly.



      Input text



      clexec.py "top -d1|grep Swap"
      NVA-0055-Prod-N08:Swap: 48G Total, 1428K Used, 48G Free
      NVA-0055-Prod-N01:Swap: 48G Total, 332K Used, 48G Free
      NVA-0055-Prod-N04:Swap: 48G Total, 169M Used, 48G Free
      NVA-0055-Prod-N05:Swap: 48G Total, 884K Used, 48G Free
      NVA-0055-Prod-N02:Swap: 48G Total, 4236K Used, 48G Free
      NVA-0055-Prod-N06:Swap: 48G Total, 132K Used, 48G Free
      NVA-0055-Prod-N07:Swap: 48G Total, 92K Used, 48G Free
      NVA-0055-Prod-N03:Swap: 48G Total, 712K Used, 48G Free


      I am using below below command



      $a= import-csv 'C:UsersPDC_AVERE_SWAP_OP.txt' -Delimiter 't' -Header 'Name','Total','used','free'
      $a | export-csv csvfile.csv -NoTypeInformation


      My output coming like below



      enter image description here



      after correcting th delimiter part I am getting all the data in a single column but I need it to split among the columns Total,used and free



      enter image description here










      share|improve this question
















      Hi I am trying to export a text file to csv format in Powershell but facing some alignment issue.data not coming properly.



      Input text



      clexec.py "top -d1|grep Swap"
      NVA-0055-Prod-N08:Swap: 48G Total, 1428K Used, 48G Free
      NVA-0055-Prod-N01:Swap: 48G Total, 332K Used, 48G Free
      NVA-0055-Prod-N04:Swap: 48G Total, 169M Used, 48G Free
      NVA-0055-Prod-N05:Swap: 48G Total, 884K Used, 48G Free
      NVA-0055-Prod-N02:Swap: 48G Total, 4236K Used, 48G Free
      NVA-0055-Prod-N06:Swap: 48G Total, 132K Used, 48G Free
      NVA-0055-Prod-N07:Swap: 48G Total, 92K Used, 48G Free
      NVA-0055-Prod-N03:Swap: 48G Total, 712K Used, 48G Free


      I am using below below command



      $a= import-csv 'C:UsersPDC_AVERE_SWAP_OP.txt' -Delimiter 't' -Header 'Name','Total','used','free'
      $a | export-csv csvfile.csv -NoTypeInformation


      My output coming like below



      enter image description here



      after correcting th delimiter part I am getting all the data in a single column but I need it to split among the columns Total,used and free



      enter image description here







      powershell csv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 21:36









      LotPings

      19.6k61633




      19.6k61633










      asked Nov 15 '18 at 15:22









      Chinmay NayakChinmay Nayak

      618




      618
























          3 Answers
          3






          active

          oldest

          votes


















          1














          If you want to drop the initial line from the file,

          you may read content as text and skip the first line before ConvertFrom-Csv



          Get-Content 'C:UsersPDC_AVERE_SWAP_OP.txt' | Select-Object -Skip 1|
          ConvertFrom-Csv -Delimiter "`t" -Header Name,Total,used,free |
          Export-Csv csvfile.csv -NoTypeInformation


          Just in case there is no tab but space(s) like above sample, you could use



          $Data = Get-Content '.PDC_AVERE_SWAP_OP.txt' |Select-Object -skip 1| ForEach-Object{
          $Name,$Total,$used,$free = ($_ -split('s+'))[0,1,3,5]
          [PSCustomObject]@{
          Name = $Name
          Total= $Total
          used = $used
          free = $free
          }
          }
          $Data
          $Data | Export-Csv '.PDC_AVERE_SWAP_OP.csv' -NoTypeInformation
          $Data | Export-Excel '.PDC_AVERE_SWAP_OP.xlsx' -AutoSize -Show


          and thereby strip off the label from each value:



          Name                    Total used  free
          ---- ----- ---- ----
          NVA-0055-Prod-N08:Swap: 48G 1428K 48G
          NVA-0055-Prod-N01:Swap: 48G 332K 48G
          NVA-0055-Prod-N04:Swap: 48G 169M 48G
          NVA-0055-Prod-N05:Swap: 48G 884K 48G
          NVA-0055-Prod-N02:Swap: 48G 4236K 48G
          NVA-0055-Prod-N06:Swap: 48G 132K 48G
          NVA-0055-Prod-N07:Swap: 48G 92K 48G
          NVA-0055-Prod-N03:Swap: 48G 712K 48G


          When using Export-Excel you'll directly get an xlsx file



          enter image description here



          EDIT: as mentioned in the comment here a variant using a RegEx to separate the elements
          See the RegEx live https://regex101.com/r/GFXpXw/1 with a missing used value



          ## Q:Test20181115SO_53322626_2.ps1

          $RE = "^(?<Name>[^ ]+) (?<Total>d+[KMG]) Total(, (?<used>d+[KMG]) Used)?, (?<free>d+[KMG])"

          $Data = Get-Content '.PDC_AVERE_SWAP_OP.txt' |Select-Object -skip 1| ForEach-Object{
          if ($_ -match $RE){
          [PSCustomObject]@{
          Name = $Matches.Name
          Total= $Matches.Total
          used = $Matches.used
          free = $Matches.free
          }
          } else {Write-Host ("{0} didn't match RegEx" -f $_)}
          }
          $Data
          $Data | Export-Csv '.PDC_AVERE_SWAP_OP.csv' -NoTypeInformation
          #$Data | Export-Excel '.PDC_AVERE_SWAP_OP.xlsx' -AutoSize





          share|improve this answer


























          • what if for some cases the used column is not there ,will the free column data moved to used column or it will come as blank ?

            – Chinmay Nayak
            Nov 16 '18 at 10:24











          • If there is no delimiter to separate data as intended the values will slide left, this could be avoided using a more complex regular expression using lookaheads and named capture groups (assuming you are using the 2nd script)

            – LotPings
            Nov 16 '18 at 10:35











          • See changed answer.

            – LotPings
            Nov 16 '18 at 12:24



















          3














          The problem is -Delimiter 't'. It literally means that letter t is the delimiter character. Most likely you mean the tab character, which in Powershell is backtick-t: `t.






          share|improve this answer































            1














            use "`t" instead of "t" as Delimeter and instead using Export-CSV you can use the following code,



             "Name,Total,used,free" | Set-Content csvfile.csv

            $a | %{ Write-Output "$($_.Name),$($_.Total),$($_.free),$($_.free)" | out-file csvfile.csv -Encoding ascii -Append -Force }





            share|improve this answer


























            • Please edit the answer and explain why out-file would be better than export-csv.

              – vonPryz
              Nov 15 '18 at 16:06











            • Usually Out-File you will give you more flexibility and more control over it. Consider a scenario, you have several different variables in a script and you need to select some properties from multiple variables and export the output to CSV.in that case you cannot use Export-CSV. Now coming to your question, sometime Export-CSV does not work as expected as in your case (don't know exact reason ) then we can use Out-File to get more control over it.@vonPryz

              – Gourango Ghosh
              Nov 15 '18 at 16:40













            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322626%2fpower-shell-csv-conversion%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            If you want to drop the initial line from the file,

            you may read content as text and skip the first line before ConvertFrom-Csv



            Get-Content 'C:UsersPDC_AVERE_SWAP_OP.txt' | Select-Object -Skip 1|
            ConvertFrom-Csv -Delimiter "`t" -Header Name,Total,used,free |
            Export-Csv csvfile.csv -NoTypeInformation


            Just in case there is no tab but space(s) like above sample, you could use



            $Data = Get-Content '.PDC_AVERE_SWAP_OP.txt' |Select-Object -skip 1| ForEach-Object{
            $Name,$Total,$used,$free = ($_ -split('s+'))[0,1,3,5]
            [PSCustomObject]@{
            Name = $Name
            Total= $Total
            used = $used
            free = $free
            }
            }
            $Data
            $Data | Export-Csv '.PDC_AVERE_SWAP_OP.csv' -NoTypeInformation
            $Data | Export-Excel '.PDC_AVERE_SWAP_OP.xlsx' -AutoSize -Show


            and thereby strip off the label from each value:



            Name                    Total used  free
            ---- ----- ---- ----
            NVA-0055-Prod-N08:Swap: 48G 1428K 48G
            NVA-0055-Prod-N01:Swap: 48G 332K 48G
            NVA-0055-Prod-N04:Swap: 48G 169M 48G
            NVA-0055-Prod-N05:Swap: 48G 884K 48G
            NVA-0055-Prod-N02:Swap: 48G 4236K 48G
            NVA-0055-Prod-N06:Swap: 48G 132K 48G
            NVA-0055-Prod-N07:Swap: 48G 92K 48G
            NVA-0055-Prod-N03:Swap: 48G 712K 48G


            When using Export-Excel you'll directly get an xlsx file



            enter image description here



            EDIT: as mentioned in the comment here a variant using a RegEx to separate the elements
            See the RegEx live https://regex101.com/r/GFXpXw/1 with a missing used value



            ## Q:Test20181115SO_53322626_2.ps1

            $RE = "^(?<Name>[^ ]+) (?<Total>d+[KMG]) Total(, (?<used>d+[KMG]) Used)?, (?<free>d+[KMG])"

            $Data = Get-Content '.PDC_AVERE_SWAP_OP.txt' |Select-Object -skip 1| ForEach-Object{
            if ($_ -match $RE){
            [PSCustomObject]@{
            Name = $Matches.Name
            Total= $Matches.Total
            used = $Matches.used
            free = $Matches.free
            }
            } else {Write-Host ("{0} didn't match RegEx" -f $_)}
            }
            $Data
            $Data | Export-Csv '.PDC_AVERE_SWAP_OP.csv' -NoTypeInformation
            #$Data | Export-Excel '.PDC_AVERE_SWAP_OP.xlsx' -AutoSize





            share|improve this answer


























            • what if for some cases the used column is not there ,will the free column data moved to used column or it will come as blank ?

              – Chinmay Nayak
              Nov 16 '18 at 10:24











            • If there is no delimiter to separate data as intended the values will slide left, this could be avoided using a more complex regular expression using lookaheads and named capture groups (assuming you are using the 2nd script)

              – LotPings
              Nov 16 '18 at 10:35











            • See changed answer.

              – LotPings
              Nov 16 '18 at 12:24
















            1














            If you want to drop the initial line from the file,

            you may read content as text and skip the first line before ConvertFrom-Csv



            Get-Content 'C:UsersPDC_AVERE_SWAP_OP.txt' | Select-Object -Skip 1|
            ConvertFrom-Csv -Delimiter "`t" -Header Name,Total,used,free |
            Export-Csv csvfile.csv -NoTypeInformation


            Just in case there is no tab but space(s) like above sample, you could use



            $Data = Get-Content '.PDC_AVERE_SWAP_OP.txt' |Select-Object -skip 1| ForEach-Object{
            $Name,$Total,$used,$free = ($_ -split('s+'))[0,1,3,5]
            [PSCustomObject]@{
            Name = $Name
            Total= $Total
            used = $used
            free = $free
            }
            }
            $Data
            $Data | Export-Csv '.PDC_AVERE_SWAP_OP.csv' -NoTypeInformation
            $Data | Export-Excel '.PDC_AVERE_SWAP_OP.xlsx' -AutoSize -Show


            and thereby strip off the label from each value:



            Name                    Total used  free
            ---- ----- ---- ----
            NVA-0055-Prod-N08:Swap: 48G 1428K 48G
            NVA-0055-Prod-N01:Swap: 48G 332K 48G
            NVA-0055-Prod-N04:Swap: 48G 169M 48G
            NVA-0055-Prod-N05:Swap: 48G 884K 48G
            NVA-0055-Prod-N02:Swap: 48G 4236K 48G
            NVA-0055-Prod-N06:Swap: 48G 132K 48G
            NVA-0055-Prod-N07:Swap: 48G 92K 48G
            NVA-0055-Prod-N03:Swap: 48G 712K 48G


            When using Export-Excel you'll directly get an xlsx file



            enter image description here



            EDIT: as mentioned in the comment here a variant using a RegEx to separate the elements
            See the RegEx live https://regex101.com/r/GFXpXw/1 with a missing used value



            ## Q:Test20181115SO_53322626_2.ps1

            $RE = "^(?<Name>[^ ]+) (?<Total>d+[KMG]) Total(, (?<used>d+[KMG]) Used)?, (?<free>d+[KMG])"

            $Data = Get-Content '.PDC_AVERE_SWAP_OP.txt' |Select-Object -skip 1| ForEach-Object{
            if ($_ -match $RE){
            [PSCustomObject]@{
            Name = $Matches.Name
            Total= $Matches.Total
            used = $Matches.used
            free = $Matches.free
            }
            } else {Write-Host ("{0} didn't match RegEx" -f $_)}
            }
            $Data
            $Data | Export-Csv '.PDC_AVERE_SWAP_OP.csv' -NoTypeInformation
            #$Data | Export-Excel '.PDC_AVERE_SWAP_OP.xlsx' -AutoSize





            share|improve this answer


























            • what if for some cases the used column is not there ,will the free column data moved to used column or it will come as blank ?

              – Chinmay Nayak
              Nov 16 '18 at 10:24











            • If there is no delimiter to separate data as intended the values will slide left, this could be avoided using a more complex regular expression using lookaheads and named capture groups (assuming you are using the 2nd script)

              – LotPings
              Nov 16 '18 at 10:35











            • See changed answer.

              – LotPings
              Nov 16 '18 at 12:24














            1












            1








            1







            If you want to drop the initial line from the file,

            you may read content as text and skip the first line before ConvertFrom-Csv



            Get-Content 'C:UsersPDC_AVERE_SWAP_OP.txt' | Select-Object -Skip 1|
            ConvertFrom-Csv -Delimiter "`t" -Header Name,Total,used,free |
            Export-Csv csvfile.csv -NoTypeInformation


            Just in case there is no tab but space(s) like above sample, you could use



            $Data = Get-Content '.PDC_AVERE_SWAP_OP.txt' |Select-Object -skip 1| ForEach-Object{
            $Name,$Total,$used,$free = ($_ -split('s+'))[0,1,3,5]
            [PSCustomObject]@{
            Name = $Name
            Total= $Total
            used = $used
            free = $free
            }
            }
            $Data
            $Data | Export-Csv '.PDC_AVERE_SWAP_OP.csv' -NoTypeInformation
            $Data | Export-Excel '.PDC_AVERE_SWAP_OP.xlsx' -AutoSize -Show


            and thereby strip off the label from each value:



            Name                    Total used  free
            ---- ----- ---- ----
            NVA-0055-Prod-N08:Swap: 48G 1428K 48G
            NVA-0055-Prod-N01:Swap: 48G 332K 48G
            NVA-0055-Prod-N04:Swap: 48G 169M 48G
            NVA-0055-Prod-N05:Swap: 48G 884K 48G
            NVA-0055-Prod-N02:Swap: 48G 4236K 48G
            NVA-0055-Prod-N06:Swap: 48G 132K 48G
            NVA-0055-Prod-N07:Swap: 48G 92K 48G
            NVA-0055-Prod-N03:Swap: 48G 712K 48G


            When using Export-Excel you'll directly get an xlsx file



            enter image description here



            EDIT: as mentioned in the comment here a variant using a RegEx to separate the elements
            See the RegEx live https://regex101.com/r/GFXpXw/1 with a missing used value



            ## Q:Test20181115SO_53322626_2.ps1

            $RE = "^(?<Name>[^ ]+) (?<Total>d+[KMG]) Total(, (?<used>d+[KMG]) Used)?, (?<free>d+[KMG])"

            $Data = Get-Content '.PDC_AVERE_SWAP_OP.txt' |Select-Object -skip 1| ForEach-Object{
            if ($_ -match $RE){
            [PSCustomObject]@{
            Name = $Matches.Name
            Total= $Matches.Total
            used = $Matches.used
            free = $Matches.free
            }
            } else {Write-Host ("{0} didn't match RegEx" -f $_)}
            }
            $Data
            $Data | Export-Csv '.PDC_AVERE_SWAP_OP.csv' -NoTypeInformation
            #$Data | Export-Excel '.PDC_AVERE_SWAP_OP.xlsx' -AutoSize





            share|improve this answer















            If you want to drop the initial line from the file,

            you may read content as text and skip the first line before ConvertFrom-Csv



            Get-Content 'C:UsersPDC_AVERE_SWAP_OP.txt' | Select-Object -Skip 1|
            ConvertFrom-Csv -Delimiter "`t" -Header Name,Total,used,free |
            Export-Csv csvfile.csv -NoTypeInformation


            Just in case there is no tab but space(s) like above sample, you could use



            $Data = Get-Content '.PDC_AVERE_SWAP_OP.txt' |Select-Object -skip 1| ForEach-Object{
            $Name,$Total,$used,$free = ($_ -split('s+'))[0,1,3,5]
            [PSCustomObject]@{
            Name = $Name
            Total= $Total
            used = $used
            free = $free
            }
            }
            $Data
            $Data | Export-Csv '.PDC_AVERE_SWAP_OP.csv' -NoTypeInformation
            $Data | Export-Excel '.PDC_AVERE_SWAP_OP.xlsx' -AutoSize -Show


            and thereby strip off the label from each value:



            Name                    Total used  free
            ---- ----- ---- ----
            NVA-0055-Prod-N08:Swap: 48G 1428K 48G
            NVA-0055-Prod-N01:Swap: 48G 332K 48G
            NVA-0055-Prod-N04:Swap: 48G 169M 48G
            NVA-0055-Prod-N05:Swap: 48G 884K 48G
            NVA-0055-Prod-N02:Swap: 48G 4236K 48G
            NVA-0055-Prod-N06:Swap: 48G 132K 48G
            NVA-0055-Prod-N07:Swap: 48G 92K 48G
            NVA-0055-Prod-N03:Swap: 48G 712K 48G


            When using Export-Excel you'll directly get an xlsx file



            enter image description here



            EDIT: as mentioned in the comment here a variant using a RegEx to separate the elements
            See the RegEx live https://regex101.com/r/GFXpXw/1 with a missing used value



            ## Q:Test20181115SO_53322626_2.ps1

            $RE = "^(?<Name>[^ ]+) (?<Total>d+[KMG]) Total(, (?<used>d+[KMG]) Used)?, (?<free>d+[KMG])"

            $Data = Get-Content '.PDC_AVERE_SWAP_OP.txt' |Select-Object -skip 1| ForEach-Object{
            if ($_ -match $RE){
            [PSCustomObject]@{
            Name = $Matches.Name
            Total= $Matches.Total
            used = $Matches.used
            free = $Matches.free
            }
            } else {Write-Host ("{0} didn't match RegEx" -f $_)}
            }
            $Data
            $Data | Export-Csv '.PDC_AVERE_SWAP_OP.csv' -NoTypeInformation
            #$Data | Export-Excel '.PDC_AVERE_SWAP_OP.xlsx' -AutoSize






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 12:23

























            answered Nov 15 '18 at 16:09









            LotPingsLotPings

            19.6k61633




            19.6k61633













            • what if for some cases the used column is not there ,will the free column data moved to used column or it will come as blank ?

              – Chinmay Nayak
              Nov 16 '18 at 10:24











            • If there is no delimiter to separate data as intended the values will slide left, this could be avoided using a more complex regular expression using lookaheads and named capture groups (assuming you are using the 2nd script)

              – LotPings
              Nov 16 '18 at 10:35











            • See changed answer.

              – LotPings
              Nov 16 '18 at 12:24



















            • what if for some cases the used column is not there ,will the free column data moved to used column or it will come as blank ?

              – Chinmay Nayak
              Nov 16 '18 at 10:24











            • If there is no delimiter to separate data as intended the values will slide left, this could be avoided using a more complex regular expression using lookaheads and named capture groups (assuming you are using the 2nd script)

              – LotPings
              Nov 16 '18 at 10:35











            • See changed answer.

              – LotPings
              Nov 16 '18 at 12:24

















            what if for some cases the used column is not there ,will the free column data moved to used column or it will come as blank ?

            – Chinmay Nayak
            Nov 16 '18 at 10:24





            what if for some cases the used column is not there ,will the free column data moved to used column or it will come as blank ?

            – Chinmay Nayak
            Nov 16 '18 at 10:24













            If there is no delimiter to separate data as intended the values will slide left, this could be avoided using a more complex regular expression using lookaheads and named capture groups (assuming you are using the 2nd script)

            – LotPings
            Nov 16 '18 at 10:35





            If there is no delimiter to separate data as intended the values will slide left, this could be avoided using a more complex regular expression using lookaheads and named capture groups (assuming you are using the 2nd script)

            – LotPings
            Nov 16 '18 at 10:35













            See changed answer.

            – LotPings
            Nov 16 '18 at 12:24





            See changed answer.

            – LotPings
            Nov 16 '18 at 12:24













            3














            The problem is -Delimiter 't'. It literally means that letter t is the delimiter character. Most likely you mean the tab character, which in Powershell is backtick-t: `t.






            share|improve this answer




























              3














              The problem is -Delimiter 't'. It literally means that letter t is the delimiter character. Most likely you mean the tab character, which in Powershell is backtick-t: `t.






              share|improve this answer


























                3












                3








                3







                The problem is -Delimiter 't'. It literally means that letter t is the delimiter character. Most likely you mean the tab character, which in Powershell is backtick-t: `t.






                share|improve this answer













                The problem is -Delimiter 't'. It literally means that letter t is the delimiter character. Most likely you mean the tab character, which in Powershell is backtick-t: `t.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 15:28









                vonPryzvonPryz

                12.9k23644




                12.9k23644























                    1














                    use "`t" instead of "t" as Delimeter and instead using Export-CSV you can use the following code,



                     "Name,Total,used,free" | Set-Content csvfile.csv

                    $a | %{ Write-Output "$($_.Name),$($_.Total),$($_.free),$($_.free)" | out-file csvfile.csv -Encoding ascii -Append -Force }





                    share|improve this answer


























                    • Please edit the answer and explain why out-file would be better than export-csv.

                      – vonPryz
                      Nov 15 '18 at 16:06











                    • Usually Out-File you will give you more flexibility and more control over it. Consider a scenario, you have several different variables in a script and you need to select some properties from multiple variables and export the output to CSV.in that case you cannot use Export-CSV. Now coming to your question, sometime Export-CSV does not work as expected as in your case (don't know exact reason ) then we can use Out-File to get more control over it.@vonPryz

                      – Gourango Ghosh
                      Nov 15 '18 at 16:40


















                    1














                    use "`t" instead of "t" as Delimeter and instead using Export-CSV you can use the following code,



                     "Name,Total,used,free" | Set-Content csvfile.csv

                    $a | %{ Write-Output "$($_.Name),$($_.Total),$($_.free),$($_.free)" | out-file csvfile.csv -Encoding ascii -Append -Force }





                    share|improve this answer


























                    • Please edit the answer and explain why out-file would be better than export-csv.

                      – vonPryz
                      Nov 15 '18 at 16:06











                    • Usually Out-File you will give you more flexibility and more control over it. Consider a scenario, you have several different variables in a script and you need to select some properties from multiple variables and export the output to CSV.in that case you cannot use Export-CSV. Now coming to your question, sometime Export-CSV does not work as expected as in your case (don't know exact reason ) then we can use Out-File to get more control over it.@vonPryz

                      – Gourango Ghosh
                      Nov 15 '18 at 16:40
















                    1












                    1








                    1







                    use "`t" instead of "t" as Delimeter and instead using Export-CSV you can use the following code,



                     "Name,Total,used,free" | Set-Content csvfile.csv

                    $a | %{ Write-Output "$($_.Name),$($_.Total),$($_.free),$($_.free)" | out-file csvfile.csv -Encoding ascii -Append -Force }





                    share|improve this answer















                    use "`t" instead of "t" as Delimeter and instead using Export-CSV you can use the following code,



                     "Name,Total,used,free" | Set-Content csvfile.csv

                    $a | %{ Write-Output "$($_.Name),$($_.Total),$($_.free),$($_.free)" | out-file csvfile.csv -Encoding ascii -Append -Force }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 15 '18 at 16:31

























                    answered Nov 15 '18 at 15:43









                    Gourango GhoshGourango Ghosh

                    264




                    264













                    • Please edit the answer and explain why out-file would be better than export-csv.

                      – vonPryz
                      Nov 15 '18 at 16:06











                    • Usually Out-File you will give you more flexibility and more control over it. Consider a scenario, you have several different variables in a script and you need to select some properties from multiple variables and export the output to CSV.in that case you cannot use Export-CSV. Now coming to your question, sometime Export-CSV does not work as expected as in your case (don't know exact reason ) then we can use Out-File to get more control over it.@vonPryz

                      – Gourango Ghosh
                      Nov 15 '18 at 16:40





















                    • Please edit the answer and explain why out-file would be better than export-csv.

                      – vonPryz
                      Nov 15 '18 at 16:06











                    • Usually Out-File you will give you more flexibility and more control over it. Consider a scenario, you have several different variables in a script and you need to select some properties from multiple variables and export the output to CSV.in that case you cannot use Export-CSV. Now coming to your question, sometime Export-CSV does not work as expected as in your case (don't know exact reason ) then we can use Out-File to get more control over it.@vonPryz

                      – Gourango Ghosh
                      Nov 15 '18 at 16:40



















                    Please edit the answer and explain why out-file would be better than export-csv.

                    – vonPryz
                    Nov 15 '18 at 16:06





                    Please edit the answer and explain why out-file would be better than export-csv.

                    – vonPryz
                    Nov 15 '18 at 16:06













                    Usually Out-File you will give you more flexibility and more control over it. Consider a scenario, you have several different variables in a script and you need to select some properties from multiple variables and export the output to CSV.in that case you cannot use Export-CSV. Now coming to your question, sometime Export-CSV does not work as expected as in your case (don't know exact reason ) then we can use Out-File to get more control over it.@vonPryz

                    – Gourango Ghosh
                    Nov 15 '18 at 16:40







                    Usually Out-File you will give you more flexibility and more control over it. Consider a scenario, you have several different variables in a script and you need to select some properties from multiple variables and export the output to CSV.in that case you cannot use Export-CSV. Now coming to your question, sometime Export-CSV does not work as expected as in your case (don't know exact reason ) then we can use Out-File to get more control over it.@vonPryz

                    – Gourango Ghosh
                    Nov 15 '18 at 16:40




















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322626%2fpower-shell-csv-conversion%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma