I am trying to echo 3 plain-text lines to a file using Powershell:
echo "#Generated" > psftp.scp
echo "put test.txt" >> psftp.scp
echo "quit" >> psftp.scp
I then use psftp.exe batch mode to run the file (executes the commands in SFTP), but psftp errors out seeing an invalid character:
psftp: unknown command " ■#"
What am I missing? I can manually type up the file in Windows Notepad and it (psftp) works. No matter what I change the first line to (#Generated
) it gets this error with the block symbol in the first part.
I've tried viewing the file in NotePad++ w/ "Show All Symbols" on, but only saw CR & LF at the end of lines which is normal.
Try using set/add-content instead of redirection. You might also need to set the encoding.
"#Generated" | set-content psftp.scp -Encoding Ascii
"put test.txt" | add-content psftp.scp -Encoding Ascii
"quit" | add-content psftp.scp -Encoding Ascii
In my case, I was piping a .ps1 into powershell in a .cmd file
type script.ps1 | powershell -ExecutionPolicy Bypass
Although the script had no errors when run in other ways, when run this way, it would throw a syntax error on the first row (a comment row) due to invalid characters.
I found out that if I saved the .ps1 file with the wScite editor, the default was "UTF-8 with BOM". I changed it to just "UTF-8", and after that piping the script to powershell worked fine.
Like Out-File, the default encoding of ">" and ">>" in Powershell 5 is UTF16LE ("Unicode"). I guess psftp can't read that? "FF FE" is the encoding signature or "BOM". How are you running psftp?
format-hex psftp.scp
Path: C:\Users\me\psftp.scp
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 FF FE 23 00 47 00 65 00 6E 00 65 00 72 00 61 00 .þ#.G.e.n.e.r.a.
00000010 74 00 65 00 64 00 0D 00 0A 00 70 00 75 00 74 00 t.e.d.....p.u.t.
00000020 20 00 74 00 65 00 73 00 74 00 2E 00 74 00 78 00 .t.e.s.t...t.x.
00000030 74 00 0D 00 0A 00 71 00 75 00 69 00 74 00 0D 00 t.....q.u.i.t...
00000040 0A 00 ..