Sunday, December 27, 2020

power shell script to export only some columns from csv file

 


This power shell script goes through the csv file and exports the column into a filename+out.csv



 Get-ChildItem -Path . -Recurse -Filter '*.csv' |

   ForEach{

   $out  = $PSItem.Name+"_out.csv"

       $PSItem |

       Select-Object -Property FullName,

       @{Name = 'FirstLineOfFile';Expression = {Get-Content -Path $PSItem.FullName -First 1}}

   Import-Csv $PSItem.FullName   | select Phone,HomePhone,Home_Phone,Phone_1,'Phone Home',PhoneNumber,HomePhoneNumber,'Phone Number'| Export-Csv -Path $out -NoTypeInformation

   }



Power shell script Save the csv headers from the file


-Recurse recurses through folders.. and   write to Out_Headers.txt 


 Get-ChildItem -Path . -Recurse -Filter '*.csv' |

ForEach{

     $PSItem |

     Select-Object -Property FullName,

     @{Name = 'FirstLineOfFile';Expression = {Get-Content -Path $PSItem.FullName -First 1}}

 } |Out-File Out_Headers.txt -Append