export-csv PowerShell

sourav dutta 231 Reputation points
2022-08-10T06:20:01.88+00:00

I have a PowerShell Script which I attached.
This script export PostgreSQL results into a file which have only one column. I am also attaching the file result.
My requirement is I want to delete the 1st row where column name are coming and also want to delete the symbol "" from the file.
Example given below. The results are like below in file
"database_name"
"database-1"
"database-2"
"database-3"

I want only like below in the file
database-1
database-2
database-3229834-test.txt229825-dbdashboard.txt

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Limitless Technology 39,916 Reputation points
    2022-08-10T13:37:05.117+00:00

    Hello,

    you can achieve the removal of the colum name with:

    get-content $file |
    select -Skip 1 |
    set-content "$file-temp"
    move "$file-temp" $file -Force

    To remove the " character use:

    Get-Content C:\Temp\FileBefore.txt -Encoding UTF8 | ForEach-Object {$_ -replace '"',''} | Out-File C:\Temp\FileAfter.txt -Encoding UTF8

    ------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer--


2 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-08-10T06:49:34.51+00:00

    Hi @sourav dutta ,

    maybe this helps. The script imports the test.txt file skipped the first line and replaced the " .

    $dbs = (Get-Content .\Junk\test.txt).Replace('"','') | Select-Object -skip 1  
    $dbs  
    

    Instead of the Get-Content you could apply the skip and replace on your variable $ds.Tables[0] and use Out-File to write the information in a file (if it's just one column that should work).

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Aung Zaw Min Thwin 306 Reputation points
    2022-08-10T06:51:50.623+00:00

    pls try below:

    ($YourData | ConvertTo-Csv -NoTypeInformation | Select -Skip 1) -replace '^"|"$' | Out-File "OutputFile.txt"  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.