1

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.

0

3 Answers 3

1

You can directly assign the output of foreach to variable $object like this:

$path1="C:\content1\test.txt"
$tableau = Get-Content $path1

$object = 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+",""
    $valeur | ConvertFrom-Csv -Header 'np', 'date'      
}  

Looks strange, but it is a common pattern in PowerShell (e. g. you can also assign from output of other control blocks like if and switch).

This works similar to assigning output of a pipeline to a variable. The output of ConvertFrom-Csv gets captured automatically and added to the array that PowerShell builds up and finally assigns to $object.

Sign up to request clarification or add additional context in comments.

Comments

1

In each turn of the foreach-loop you set $Object, thus overwriting the previous value.

Move the last line inside the loop and you will get the values.

$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
} 

2 Comments

Although this outputs the lines correctly, OP wants the result in $Object.
Zett42 is right, this outputs the lines correctly but the result in not in $object and this is the goal of the question
0

How about just this...

$RecordList = (Get-Content 'D:\Temp\TestFile.txt'| 
Select-Object -First 16) -replace "\s+","," | 
ConvertFrom-Csv -Header np, date
# Results
<#
np date    
-- ----    
1  21/05/20
2  21/05/20
3  21/05/20
...
#>

 $RecordList.np
# Results
<#
1
2
3
...
#>

 $RecordList.date
 # Results
<#
21/05/20
21/05/20
21/05/20
...
#>

Or

(Get-Content 'D:\Temp\TestFile.txt'| 
Select-Object -First 16) -replace "\s+","," | 
ForEach {
    [pscustomobject]@{
        np   = ($PSItem -split ',')[0]
        date = ($PSItem -split ',')[1]
    }

}
# Results
<#
np date    
-- ----    
1  21/05/20
2  21/05/20
3  21/05/20
...
#>

2 Comments

thanks! I will try this too!!very very useful!
No worries, PS always provides multiple ways to get stuff done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.