0

I want to run the cmd select-string in cmd prompt to find some string recursive.

But it can't work while the cmd have the pipeline(|) character, and it will work by running the cmd in the powershell prompt.

enter image description here

The Chinese error info is:

'select-string' is not recognized as an internal or external command,
operable program or batch file.

Can anyone know how to fix this? Make the pipeline sense in the cmd prompt or use select-string to find string recursive.

I want to make it work in my case, thanks a lot.

Victor Lee
  • 2,467
  • 3
  • 19
  • 37
  • 1
    You are calling the pipeline object of select-string in cmd prompt and not directly in powershell. Open Powershell and try it, it will work. – Ranadip Dutta Nov 29 '18 at 09:06
  • Such as the picture, run in powershell it will work, but I only want to run the command in cmd prompt. – Victor Lee Nov 29 '18 at 09:21

1 Answers1

2

As suggested in the comment, you need to use pipe in PowerShell, not in cmd. You can do it like this:

powershell "Get-ChildItem | Select-String -Pattern 'something something'"

Note: Of course it's just a workaround, I'd still suggest to just run PowerShell cmdlets in PowerShell, not in cmd. It can save you from the headache of troubleshooting non-working code.

Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34
  • Thanks, I run this command in the salt cmd.run, like this: salt -G 'os:windows' cmd.run 'powershell "get-childitem -recurse -path c:\ -include *.ini | select-string -pattern 'some string'"', now it works well, is there has some other way to call the powershell by using salt cmd.run? – Victor Lee Dec 03 '18 at 02:39
  • 1
    @VictorLee I'm not a SaltStack expert but usually you can run PowerShell scripts using `powershell.exe C:\path\to\script.ps1`. If you do so, remember to set appropriate ExecutionPolicy (see [this article](https://www.mssqltips.com/sqlservertip/2702/setting-the-powershell-execution-policy/)). – Robert Dyjas Dec 03 '18 at 07:41