Putting this here, since it is too long for a normal comment.
Ditto to 'mklement0' helpful answer.
Yet, there are always different ways to deal with one thing or the other, and the choice is yours.
If you want an array, why not just start with one? @(1,2,3) Right now you are using a single string and splitting it.
Again, 'mklement0', is the most direct, but, here is what I mean, because you say, you are creating the file, not that you are reading a file you are given, thus you have complete control of this creation effort:
(again, just giving you food for thought):
# Create a new array, assigning the results to a variable and output to screen
($array = @(1,2,3))
# Or as a string set
($array = @('1','2','3'))
# Results using either of the above, no extra splittng required
<#
1
2
3
#>
$array.Count
# Results
<#
3
#>
# Create a text file of the results
# Remove existing file, if it exists
Remove-Item -Path 'modules.txt' -ErrorAction SilentlyContinue
$array |
ForEach-Object {Add-Content -Path 'modules.txt' -Value $PSitem}
Get-Content -Path 'modules.txt'
# Results
<#
1
2
3
#>
# Or using what you have
# Remove existing file, if it exists
Remove-Item -Path 'modules.txt' -ErrorAction SilentlyContinue
'1,2,3' -split ',' |
ForEach-Object {Add-Content -Path 'modules.txt' -Value $PSitem}
Get-Content -Path 'modules.txt'
# Results
<#
1
2
3
#>
# Here is another way, using what you show, using string -replace vs string split
# Remove existing file, if it exists
Remove-Item -Path 'modules.txt' -ErrorAction SilentlyContinue
($array = ('1,2,3')) -replace ',',"`r`n" |
Out-File -FilePath 'modules.txt'
Get-Content -Path 'modules.txt'
# Results
<#
1
2
3
#>
# Remove existing file, if it exists
Remove-Item -Path 'modules.txt' -ErrorAction SilentlyContinue
$array = '1,2,3' -replace ',',"`r`n" |
Out-File -FilePath 'modules.txt'
Get-Content -Path 'modules.txt'
# Results
<#
1
2
3
#>