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
You need to process the elements of array
$SplitNameindividually, 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-ContentSet-Contentquietly overwrites an existing file, whichNew-Itemonly does if you add-Force.As for what you tried:
This assigns a script block (
{ ... }) (needlessly wrapped in(...)) to$Content, whereas you're looking for a string:$Content + $Splitnameis an expression, and in order for an expression to function as a command argument, it must be enclosed in(...):Since
$SplitNameis a string you want to split, there's no reason to involveGet-ChildItem; apart from that, the statement has the same problem as described above: expression$Filename -split ","lacks(...)enclosure.