Q:

Powershell script: works when written by hand, doesn't work when generated.

I have a disk operation which I would like to automate with a Powershell script called test.ps1 which looks like this:
diskpart /s diskstuff.txt

The file diskstuff.txt looks like this:
list disk
select disk 0
list partition

When I create diskstuff.txt by hand using notepad.exe, and then run it like this:
PS C:\> powershell.exe .\test.ps1

It runs fine, giving me information about the partitions. But I want just one file to deal with. So I tried this script in test2.ps1:
$text1 = 'list disk'
$text2 = 'select disk 0'
$text3 = 'list partition'
$text1 > 'diskstuff2.txt'
$text2 >> 'diskstuff2.txt'
$text3 >> 'diskstuff2.txt'
diskpart /s diskstuff2.txt

When I ran the script with:
PS C:\> powershell.exe .\test2.ps1

It didn't return anything useful from diskpart. I inspected diskstuff2.txt and diskstuff.txt in notepad.exe, and it looked exactly the same. 
I looked around the 'net a bit, and found a better way to inspect the files. First a dump of the handcoded file (diskstuff.txt). 
PS C:\> Get-Content diskstuff.txt -Encoding Byte

The last few lines of the result were
116
105
111
110
13
10

Then a dump of the generated file (diskstuff2.txt)
PS C:\> Get-Content diskstuff.txt -Encoding Byte

The last few lines of the result were:
116
0
105
0
111
0
110
0
13
0
10
0

This showed that my script to generate diskstuff2.txt was somehow inserting zeros between every character. Is there a way I can get my script to generate diskstuff2.txt without all of those zeros, so that I can execute test2.ps1 as intended?

A:

Redirection operators > and >> use UTF-16 encoding for files. If you need different encoding, then you should use Out-File or Set-Content/Add-Content cmdlets. All have -Encoding parameter to specify encoding.
--------update by Jay-----------
This was the correct approach. The final answer for test2.ps1 was:
$text1 = 'list disk'
$text2 = 'select partition 0'
$text3 = 'list partition'
Set-Content -Path diskstuff2.txt -Value $text1 -Encoding UTF8
Add-Content -Path diskstuff2.txt -Value $text2 -Encoding UTF8
Add-Content -Path diskstuff2.txt -Value $text3 -Encoding UTF8
diskpart /s diskstuff2.txt

