I want to create and display a powershell object with two columns (two properties: np and date) with 15 lines from a file "test.txt".
1 21/05/20 2 21/05/20 3 21/05/20 4 21/05/20 5 21/05/20 6 21/05/20 7 21/05/20 8 21/05/20 9 21/05/20 10 21/05/20 11 21/05/20 12 21/05/20 13 21/05/20 14 21/05/20 15 21/05/20
Here is my code:
$path1="C:\content1\test.txt"
$tableau = Get-Content $path1
Foreach($valeur in $tableau) {
# trim spaces at beginning and end
$valeur = $valeur.trim()
# insert , at specific places for ConvertFrom-CSV command
$valeur = $valeur.insert(2,",")
$valeur = $valeur -replace "\s+",""
$Object = $valeur | ConvertFrom-Csv -Header 'np', 'date'
}
$Object
The output of my code is:
np date -- ---- 15 21/05/20
I have only the last line in my object $Object whereas I want the 15 lines of my .txt file.