[POWERSHELL] How do you put an element in a specific place in an array

Hello, i asked this question because I’m blocked !

I’ll explain you the situation. in the folder name C:\test i have 2 directory.

in the first directory I have one .txt name sentence.txt with the sentence “disovery of new planets”

in the second directory I have an other .txt name sentence.txt with the sentence “it’s difficult to discover planet”

I’ll to put each sentence in my column named “Sentence” but i don’t know how i can do it I try like this but without success :


$path= Get-ChildItem "C:\test"

try {

for ($i=0 ; $i -lt $path.Count ; $i++){

$result = $path| Select-Object -Index $i
$source = "C:\test\sentence.txt"

$Data = Get-Content -Path $source
$sentence= $Data[0].Split(“r”)`

<# $Display2 = $chemins | ForEach-Object -Process { Add-Member -InputObject $_ -MemberType NoteProperty -Name Commentaire -Value ($sentence) -PassThru } | Format-Table -Property Sentencecolumn -AutoSize #>

<# function array { $d = New-Object PSObject $d | Add-Member -Name Creation -MemberType NoteProperty -Value $new_name $d | Add-Member -Name ID -MemberType NoteProperty -Value $i $d | Add-Member -Name Commentaire -MemberType NoteProperty -Value $sentence return $d } $Myarray=@() $Myarray+= ForEach-Object -Begin{Select-Object -Index $i} -Process{ array -Nom "$sentence[$i]" } #>




but Each time my column Sentencecolumn is filled by the contained of the last directory but i don’t know why !

Can you help me ? thanks !



# Recreate environment
md c:\test, C:\test\folder1, C:\test\folder2 -EA 0 
“disovery of new planets” | Out-File C:\test\folder1\Sentence.txt
“it’s difficult to discover planet” | Out-File C:\test\folder2\Sentence.txt

# Script
$RootFolder = 'c:\test'
$FileName = 'Sentence.txt'
$myOutput = foreach ($Subfolder in (Get-ChildItem $RootFolder -Directory)){
    try {
        $Content = Get-Content "$($Subfolder.FullName)\$FileName" -EA 1 
        New-Object -TypeName PSObject -Property @{
            FileName = "$($Subfolder.FullName)\$FileName"
            Sentence = $Content
    } catch {
        Write-Warning "No '$FileName' file found under '$($Subfolder.FullName)' folder"
$myOutput | FT -a 

Thanks for the help i gonna try that