Run PowerShell command (with pipeline) in CMD
Asked Answered
C

2

8

When we run the following command in PowerShell:

get-date | sc C:\temp\date.log

it creates date.log file with current date.

But if we run the same via CMD:

powershell get-date | sc C:\temp\date.log

It complains:

ERROR: Unrecognized command

DESCRIPTION:
        SC is a command line program used for communicating with the
        Service Control Manager and services.
USAGE:
        sc <server> [command] [service name] <option1> <option2>...

Apparently, CMD confuses pipeline meant for POSH, with its own.

Can anyone point me how to make it run via CMD?

Thanks in anticipation.

Cofield answered 27/12, 2013 at 13:56 Comment(0)
W
17
powershell -Command " & {Get-Date | sc c:\tmp\date.log}"

CMD and PowerShell

Wellheeled answered 27/12, 2013 at 14:23 Comment(2)
Broken link here.Vaunt
web.archive.org/web/20140206160933/http://technet.microsoft.com/…Wellheeled
B
0

As long as the pipe symbol is escaped from cmd with double-quotes, it will work. Sc is an unfortunate alias, since there's an sc.exe command in windows. ? or where-object might be easier than trying to embed an extra set of double-quotes in a query.

powershell "get-date | set-content c:\tmp\date.log"

powershell "get-ciminstance win32_volume | ? label -like 'month *' | select label"
Baynebridge answered 7/3, 2022 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.