You need to process the elements of array $SplitName
individually, which you can do by enumerating it in the pipeline ...
... processing each element via ForEach-Object
...
... and saving the results to an output file with Set-Content
- Note that
Set-Content
quietly overwrites an existing file, which New-Item
only does if you add -Force
.
$Filename = "Jessica,Jimmy,Adam"
$Content = 'Whoisthesmartest=' # Note: Use a *string*, not {...}
$Splitname = $Filename -split "," # No need for Get-ChildItem - unless I'm missing something.
$Splitname |
ForEach-Object { $Content + $_ } |
Set-Content c:\test.txt
As for what you tried:
$Content = ({Whoisthesmartest=})
This assigns a script block ({ ... }
) (needlessly wrapped in (...)
) to $Content
, whereas you're looking for a string:
$Content = 'Whoisthesmartest='
... -value $Content + $Splitname
$Content + $Splitname
is an expression, and in order for an expression to function as a command argument, it must be enclosed in (...)
:
... -value ($Content + $Splitname)
$Splitname = get-childitem $Filename -split ","
Since $SplitName
is a string you want to split, there's no reason to involve Get-ChildItem
; apart from that, the statement has the same problem as described above: expression $Filename -split ","
lacks (...)
enclosure.