2

Consider:

(Get-Content Rel_DDL.SQL) | ForEach-Object { 
  $_ -replace "SWIFT [\d\.]+", "SWIFT 2.4.0" 
} | Set-Content Rel_DDL.SQL

The above PowerShell code replaces SWIFT 2.3.0 with SWIFT 2.4.0 in a SQL file, which when I run through PowerShell works fine.

I want to run the PowerShell command through Windows CMD, but I am getting errors.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 5
    Possible duplicate of [Run PowerShell command from command prompt (no ps1 script)](https://stackoverflow.com/questions/18454653/run-powershell-command-from-command-prompt-no-ps1-script) – bgfvdu3w Oct 03 '17 at 06:12
  • Can you describe more your setup? Enhance your question with file locations, prompts when executing from powershell and cmd. – Alex Sarafian Oct 03 '17 at 07:50

2 Answers2

2

You can use the Powershell.exe command in CMD Windows with the -command parameter. Did you try it?

-Command

Executes the specified commands (and any parameters) as though they were
typed at the Windows PowerShell command prompt, and then exits, unless
NoExit is specified. The value of Command can be "-", a string. or a
script block.

If the value of Command is "-", the command text is read from standard input. If the value of Command is a script block, the script block must be enclosed in braces ({}).

You can specify a script block only when running PowerShell.exe in Windows PowerShell. The results of the script block are returned to the parent shell as deserialized XML objects, not live objects.

If the value of Command is a string, Command must be the last parameter in the command, because any characters typed after the command are interpreted as the command arguments.

To write a string that runs a Windows PowerShell command, use the format: "& {}" where the quotation marks indicate a string and the invoke operator (&) causes the command to be executed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohammad Barghamadi
  • 107
  • 1
  • 1
  • 10
1

Use the powershell command in that .bat script. I would not write to the same file as used for the input. Yes, I know this works because Get-Content reads the entire file, but it is not a good practice.

powershell -NoProfile -Command "(Get-Content Rel_DDL.SQL) |" ^
    "ForEach-Object { $_ -replace 'SWIFT [\d\.]+', 'SWIFT 2.4.0' } |" ^
    "Out-File -FilePath Rel_DDL2.SQL -Encoding Default"
lit
  • 14,456
  • 10
  • 65
  • 119