convert text to csv

Hi

Am trying to convert text file, that containing this.

dsafdsafds1
fdasfasdfa2
wqewqreerw3
xcvzxcvxzc4
uyiuoyiuoy5
hjklhjlhkl6
nmnmmmmnmm7

My Powershell file can read the text file and export to CSV but I am doing something wrong.

Add-Content -Path C:\Temp\Employees.csv  -Value 'Number, UserID'
$employees = @()

$stream_reader = New-Object System.IO.StreamReader{C:\temp\userlist.txt}
$numberOfValues = 0
while (($current_line =$stream_reader.ReadLine()) -ne $null)
{
#Calls on function to collect information from AD
#CollectValuesFromAD
Write-Host "$current_line"
$numberOfValues++


  $employees = @($numberOfValues, $current_line)
  $employees | foreach { Add-Content -Path  C:\Temp\Employees.csv -Value $_ }
  
}
Write-Host $numberOfValues "user was found in the file!";

The result is like this in my CSV file

Number UserID
1
dsafdsafds1
2
fdasfasdfa2
3
wqewqreerw3
4
xcvzxcvxzc4
5
uyiuoyiuoy5
6
hjklhjlhkl6
7
nmnmmmmnmm7
As you can see, "UserID" values are ending up under Number, and not to the right of Number.

pls provide sample text files, sample desired output, and explain what you want to do - convert what to what?

I’d say make it simple and easy on yourself by putting powershell to work for you.

$stream_reader = New-Object System.IO.StreamReader -ArgumentList C:\temp\userlist.txt

$numberOfValues = 0

$employees = while ($current_line = $stream_reader.ReadLine())
{
    Write-Host $current_line

    [PSCustomObject]@{
        UserName = $current_line
        ID       = ++$numberOfValues
    }
}

$employees | Export-Csv -Path C:\Temp\Employees.csv -NoTypeInformation

Write-Host $numberOfValues user was found in the file!

This can be accomplished with a simple for loop as well:

#Emulate content list
$content = @"
dsafdsafds1
fdasfasdfa2
wqewqreerw3
xcvzxcvxzc4
uyiuoyiuoy5
hjklhjlhkl6
nmnmmmmnmm7
"@ -split [environment]::NewLine

#$content = Get-Content C:\temp\userlist.txt

$results = for ($i=0;$i -lt $content.Count;$i++) {
    [pscustomobject]@{
        UserName = $content[$i]
        ID       = ($i + 1)
    }
}

$results | Export-Csv -Path C:\Temp\Employees.csv -NoTypeInformation

Output:

PS C:\Users\rasim> $results


UserName    ID
--------    --
dsafdsafds1  1
fdasfasdfa2  2
wqewqreerw3  3
xcvzxcvxzc4  4
uyiuoyiuoy5  5
hjklhjlhkl6  6
nmnmmmmnmm7  7

[quote quote=282601]pls provide sample text files, sample desired output, and explain what you want to do – convert what to what?

[/quote]
Hi

The text file (simple notepad .txt) named userlist.txt Contains
dsafdsafds1
fdasfasdfa2
wqewqreerw3
xcvzxcvxzc4
uyiuoyiuoy5
hjklhjlhkl6
nmnmmmmnmm7

 

The result I want to achieve is

Number Userid
1 dsafdsafds1
2 fdasfasdfa2
3 wqewqreerw3
4 xcvzxcvxzc4
5 uyiuoyiuoy5
6 hjklhjlhkl6
7 nmnmmmmnmm7
 

The goal with this is

The text file is a symbolic file with pretend user users.
That I pull information about from AD (I do not have access to Active Directory so I will add that part later to the script).
The result will be exported to a CSV file.

But I can’t get the userid and number on the same row.
As a start thing, until I get back to work, when I do that I will start adding “fetch information from Active Directory”.

 

Thank you,
That was exactly what I want to achieve, and the code looks so much better, thanks to you.
:slight_smile:

I changed places on
ID = ++$numberOfValues
UserName = $current_line

Merry Christmas and Happy New Year