Welcome › Forums › General PowerShell Q&A › convert text to csv
- This topic has 5 replies, 4 voices, and was last updated 3 weeks, 1 day ago by
Participant.
-
AuthorPosts
-
-
December 27, 2020 at 6:47 pm #282592
Hi
Am trying to convert text file, that containing this.
dsafdsafds1
fdasfasdfa2
wqewqreerw3
xcvzxcvxzc4
uyiuoyiuoy5
hjklhjlhkl6
nmnmmmmnmm7My Powershell file can read the text file and export to CSV but I am doing something wrong.
PowerShell123456789101112131415161718Add-Content -Path C:\Temp\Employees.csv -Value 'Number, UserID'$employees = @()$stream_reader = New-Object System.IO.StreamReader{C:\temp\userlist.txt}$numberOfValues = 0while (($current_line =$stream_reader.ReadLine()) -ne $null){#Calls on function to collect information from AD#CollectValuesFromADWrite-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.
-
This topic was modified 3 weeks, 2 days ago by
08Gamer75.
-
This topic was modified 3 weeks, 2 days ago by
-
December 27, 2020 at 6:53 pm #282601
pls provide sample text files, sample desired output, and explain what you want to do – convert what to what?
-
December 27, 2020 at 9:02 pm #282607
I’d say make it simple and easy on yourself by putting powershell to work for you.
PowerShell1234567891011121314151617$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_lineID = ++$numberOfValues}}$employees | Export-Csv -Path C:\Temp\Employees.csv -NoTypeInformationWrite-Host $numberOfValues user was found in the file! -
December 28, 2020 at 9:43 am #282685
This can be accomplished with a simple for loop as well:
PowerShell123456789101112131415161718192021#Emulate content list$content = @"dsafdsafds1fdasfasdfa2wqewqreerw3xcvzxcvxzc4uyiuoyiuoy5hjklhjlhkl6nmnmmmmnmm7"@ -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 -NoTypeInformationOutput:
PowerShell123456789101112PS C:\Users\rasim> $resultsUserName ID-------- --dsafdsafds1 1fdasfasdfa2 2wqewqreerw3 3xcvzxcvxzc4 4uyiuoyiuoy5 5hjklhjlhkl6 6nmnmmmmnmm7 7 -
December 28, 2020 at 11:58 am #282733
pls provide sample text files, sample desired output, and explain what you want to do – convert what to what?
Hi
The text file (simple notepad .txt) named userlist.txt Contains
dsafdsafds1
fdasfasdfa2
wqewqreerw3
xcvzxcvxzc4
uyiuoyiuoy5
hjklhjlhkl6
nmnmmmmnmm7The 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”. -
December 28, 2020 at 12:04 pm #282739
$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!
Thank you,
That was exactly what I want to achieve, and the code looks so much better, thanks to you.
🙂I changed places on
ID = ++$numberOfValues
UserName = $current_lineMerry Christmas and Happy New Year
-
-
AuthorPosts
- You must be logged in to reply to this topic.