1

I need to create scripts that manipulate files and their contents. If I use Windows Powershell, the following command perfectly works:

(Get-Content my_file.txt).Replace("aaa","bbb") | Set-Content my_file.txt

This command will replace any occurence of aaa by bbb. Yet, I would like to wrap this into a script file that I named rename.cmd. The content of the file is

CMD (Get-Content my_file.txt).Replace("aaa","bbb") | Set-Content my_file.txt

Now, if I type .\rename.cmd in PowerShell, I get the following error:

PS C:\Users\me\> .\rename.cmd
'Set-Content' is not recognized as an internal or external command,
operable program or batch file.
3
  • 3
    Your mismatching two different script languages. You store your PowerShell code as batch, this will not work. Save your PowerShell code as ps1 file and run that. Commented Dec 21, 2022 at 14:46
  • If it is just one line of powershell in a batch script, why not just execute the command directly instead of calling a ps1 file? Commented Dec 21, 2022 at 14:47
  • As a single line command in a batch file, you can use @%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "(Get-Content 'my_file.txt').Replace('aaa','bbb') | Set-Content 'my_file.txt'", or @%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "(Get-Content \"my_file.txt\").Replace(\"aaa\",\"bbb\") | Set-Content \"my_file.txt\"". Commented Dec 21, 2022 at 16:17

1 Answer 1

2

A 'CMD' file is for CMD scripts (/the WinNT extension of 'BAT'/Batch files/scripts) - run by 'Cmd.exe' (/previously by 'Command.com' in the distant DOS past).

You need to change your script to have a 'PS1' file extension, so it's named 'Rename.ps1' instead - so it will be run by the correct interpreter/shell - 'PowerShell.exe'.

(But then you might then reach the point of wanting to declare/pass-in arguments to your script.)

Sign up to request clarification or add additional context in comments.

3 Comments

As is mentioned above, you can also execute powershell commands in a batch script with a .cmd (or .bat) extension, if you have some reason to do so. Such commands can have the format of %__APPDIR__%windowspowershell\v1.0\powershell.exe -noprofile -ExecutionPolicy Bypass /command "your_powershell_commands_here"
@Qwerty Indeed. But you might be better starting with 'RemoteSigned' as the execution-policy - at least in terms of providing others a command to run, and similar to the other arguments you can use a '-'/hyphen for the 'Command' argument, i.e. '-Command'. (I'm guessing that in a few cases you might want to run it against a profile.)
Thanks for the answer. I was not aware of a difference between .bat, .cmd and .ps1 extensions until today. I come from the UNIX world where most of frameworks are quite uniformly standardized and I'm quite impressed of Microsoft historical choices.

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.