Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command to run PowerShell command from Windows CMD

Tags:

powershell

cmd

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.

like image 209
Harshad Solanki Avatar asked Mar 20 '26 16:03

Harshad Solanki


1 Answers

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.

like image 129
Mohammad Barghamadi Avatar answered Mar 22 '26 05:03

Mohammad Barghamadi