0

My batch script looks like this:

@echo off
echo.
echo Replacing instances of type="module" in index.html with type="text/javascript"
cd www

(Get-Content index.html).Replace("module", "javascript") | Set-Content index.html

When I try to use .Replace in powershell, it works fine, but run from a batch script it fails with the error .Replace("module" was unexpected at this time.

(Get-Content index.html) -replace 'module', 'javascript' | Set-Content index.html

also came up with same error. Thanks in advance for any help.

1
  • 6
    That's because batch/cmd is not PowerShell Commented Jul 26, 2019 at 13:50

2 Answers 2

2

If you really want to call powershell from a .bat script, here's a way. The output file would have to be different from the input file. The "> index2.html" part is run by cmd, so it's encoded in ascii, not unicode.

type index.html | powershell $input -replace 'module','javascript' > index2.html

Or to keep it completely within powershell. The file encoding would be "ansi".

powershell "(get-content index.html) -replace 'module','javascript' | set-content index.html"
Sign up to request clarification or add additional context in comments.

Comments

1

As you cannot expect cmd.exe to run powershell.exe commands without telling to use for them, here's an answer based upon that:

@If Not Exist "www\index.html" (GoTo :EOF)Else CD "www"
@Echo(
@Echo Replacing instances of 'type="module"' in index.html with 'type="text/javascript"'.
@"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "(Get-Content -Path 'index.html') -Replace 'type=\"module\"', 'type=\"text/javascript\"' | Set-Content -Path 'index.html'"

The command line itself could also be shortened a little, if necessary:

@PowerShell -NoP "(GC 'index.html') -Replace 'type=\"module\"','type=\"text/javascript\"'|SC 'index.html'"

The above example assumes that the location of your installed PowerShell executable is the current directory, or one of those listed under %PATH%, and if not in the current directory, there is no extensionless file named powershell in the current directory or one with an extension listed under %PATHEXT%.

If you wanted to use the .Replace syntax, which requires at least it is effectively the same method:

@PowerShell -NoP "(GC 'index.html').Replace('type=\"module\"','type=\"text/javascript\"')|SC 'index.html'"

However, if you're using +, you may find that it's more efficient using the -Raw option with Get-Content:

@PowerShell -NoP "(GC 'index.html' -Raw).Replace('type=\"module\"','type=\"text/javascript\"')|SC 'index.html'"

The answers above are performing a search for the string, type="module" and replacing it with type="text/javascript". If you wish to use something less robust/more simple, like replacing module with javascript, then use -Replace 'module','javascript', or .Replace('module','javascript') instead.

Should you be aware that your file is, for instance, UTF-8 encoded, you could maintain that encoding by stipulating it as further options to Get-Content and Set-Content.

Any PowerShell version:

@"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "(Get-Content -Path 'index.html' -Encoding UTF8) -Replace 'type=\"module\"', 'type=\"text/javascript\"' | Set-Content -Path 'index.html' -Encoding UTF8"

…and shortened:

@PowerShell -NoP "(GC 'index.html' -En UTF8) -Replace 'type=\"module\"','type=\"text/javascript\"'|SC 'index.html' -En UTF8"

PowerShell version 3+:

@"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "(Get-Content -Path 'index.html' -Encoding UTF8 -Raw).Replace('type=\"module\"', 'type=\"text/javascript\"') | Set-Content -Path 'index.html' -Encoding UTF8"

…and shortened:

@PowerShell -NoP "(GC 'index.html' -En UTF8 -Ra).Replace('type=\"module\"','type=\"text/javascript\"')|SC 'index.html' -En UTF8"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.