PowerShell pipe into find.exe command

powershell

Just curious, why does this happen? If I run:

netstat -an | find "443"

into a command prompt, "443" connections are displayed ok. If I run the same command in a PowerShell console or ISE, I get the error "FIND: Parameter format not correct". Is netstat output not being piped properly to find in PS?

Note: If I run netstat -an | findstr "443" or netstat -an | select-string "443" in PS those work as expected.

Best Answer

PowerShell evaluates the content within double quotes to perform any variable expansion, sub-expressions, etc, then it discards those double quotes. What PowerShell returns from "443" is literally 443 (note the missing quotes). FIND.EXE requires the search string enclosed with double quotes.

If you want to prevent PowerShell from stripping the double quotes use the grave accent (`) to escape them.

netstat -a -n  | find `"443`"

You may also use the --% parameter to perform the escape. Requires PowerShell 3+.

nestat -a -n | find --% "443"