that is, the script should provide the license2.txt and then the powershell should filter the words name | Key material but I can't execute the command powershell
(Get-content .\Licence2.txt) -replace "(</name|</keyMaterial )", "" >Licence.txt
In order to execute a Windows PowerShell command from cmd.exe
/ a batch file, you need to call the former's CLI, powershell.exe
, documented in about_PowerShell.exe (for PowerShell (Core) v6+, it is pwsh.exe
- see about_Pwsh).
The general approach to passing arbitrary PowerShell commands to powershell.exe
from cmd.exe
is:
Enclose the command(s) in "..."
overall, and pass that string to the -Command
(-c
) parameter (which is powershell.exe
's implied parameter, though note that pwsh.exe
now defaults to -File
(-f
) instead, so it's better to be explicit).
Escape any "
characters that are part of the command(s) as \"
[1], or, if feasible, avoid embedded "
quoting altogether and use embedded '...'
quoting (verbatim PowerShell strings) that doesn't require escaping inside "..."
at all.
Therefore, in your case:
powershell -c "(Get-Content .\Licence2.txt) -replace '</name|</keyMaterial ' > Licence.txt"
Note that I've omitted the unnecessary (...)
from the regex passed to the -replace
operator, as well as the unnecessary ""
replacement operand (replacing what matched with the empty string is the default).
Additionally, it is good practice to precede the -Command
(-c
) or -File
(-f
) parameter with -NoProfile
, so as to suppress the unnecessary and potentially side-effect-inducing loading of the profile files, which are primarily intended for interactive sessions.
[1] If this fails (which happens if the characters between the \"
delimiters contain cmd.exe
metacharacters such as &
or |
), use "^""
with powershell.exe
, and ""
with pwsh.exe
.