In a Windows 7 batch file, I can call powershell v2 to create a string and then assign that string to a batch file variable to later use within another command, like this:
for /f "delims=" %%a in ('powershell -noninteractive -NoProfile -Command "Get-Date -format 'yyyy-MM-dd HH_mm_ss'"') do set "datetime=%%a"
rem process file1.txt here
ren file1.txt "newname %datetime%.txt"
If I try to do the same thing (within a batch file), in a simpler way, it doesn't work:
Editor's note: Turns out it does work - the inner " instances are correctly escaped as \".
powershell -noninteractive -NoProfile -Command "$datetime_string = Get-Date -format 'yyyy-MM-dd HH_mm_ss'; (gc file1.txt) -join [char]10 | Out-File -Encoding Ascii \"newname $datetime_string.txt\""
Can you help?
\": you're not misunderstanding: when PowerShell is invoked from the outside world (such as from a batch file),"chars. must indeed be escaped as\"- which is surprising and little-known, given that within PowerShell you must embed"chars. inside"..."as`"^was the command prompt escape. Beg 'pardon.^only works when it is used unquoted. Inside a double-quoted string (fromcmd's perspective), the only thing thatcmditself recognizes is"", which, however, is only recognized by MS-compiled programs and, with limitations, by batch files. For the whole sordid tale, see this answer of mine.