Reputation: 37
I am trying to make a txt file and apply some content to a multiple string
this is what I did so far:
$Filename = "Jessica,Jimmy,Adam"
$Content = ({Whoisthesmartest=})
$Splitname = get-childitem $Filename -split ","
new-item c:\test.txt -itemtype file -value $Content + $Splitname
But that didn't work.
what I'm trying to achieve is a txt file that has content:
Whoisthesmartest=Jessica
Whoisthesmartest=Jimmy
Whoisthesmartest=Adam
Upvotes: 1
Views: 137
Reputation: 437428
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
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.
Upvotes: 0