95

I am using PowerShell and am trying to run the following command:

.\test_cfdp.exe < test.full | tee test.log

test.full is a script that mimics command line inputs to test_cfdp.exe. However, I get the following error:

The '<' operator is reserved for future use.

Is there another way (i.e. cmdlet) I can use to get this command to work in PowerShell?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Blade3
  • 4,140
  • 13
  • 41
  • 57

6 Answers6

95

This was not supported in PowerShell v1 [and as of v5, it's still not...]

An example workaround is:

Get-Content test.full | .\test_cfdp.exe | tee test.log
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • You can use the alias `cat` to get file content easier. – iTzVoko Jun 01 '23 at 13:51
  • @aliayed Yes, or `gc` or `type`; however, it's common convention in forums etc to use the canonical cmdlet name explicitly, as anyone that knows the alias will cope, but the cmdlet name is far more searchable (if someone has a good link for an article that explains this, happy to edit it into this comment) – Ruben Bartelink Jun 01 '23 at 15:01
63

Also try:

cmd /c '.\test_cfdp.exe < test.full | tee test.log'
earGrowth
  • 761
  • 5
  • 10
  • 1
    `tee` is an alias for PowerShell's `Tee-Object` cmdlet, though, so this won't work the same/at all. – Lance U. Matthews Sep 20 '22 at 01:13
  • this doesn't work at all, unless there's a real `tee.exe` or `tee.bat`... in `%PATH%`, because `tee` isn't available in cmd. It only works in PowerShell by default where `tee` is an alias – phuclv Nov 05 '22 at 11:39
  • yes - running the command in Command Prompt works fine. This error is misleading. – kiev Apr 14 '23 at 16:13
6

In version 7 of PowerShell, you still need to use Get-Content to get the contents of an item in the specified location. For example, if you want to load a file into a Python script and write the result to a file. Use this construct:

PS > Get-Content input.txt | python .\skript.py > output.txt

Or with displayed and saved in a file:

PS > Get-Content input.txt | python .\skript.py | tee output.txt

Or switch to cmd to use the '<' operator:

C:\>python .\skript.py < input.txt > output.txt
Szczerski
  • 839
  • 11
  • 11
2

In case PowerShell is not mandatory , running the command in Command Prompt works fine.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 01 '22 at 14:36
  • That has already been proposed in [this earlier and much-upvoted answer](https://stackoverflow.com/a/16548699/150605). – Lance U. Matthews Sep 20 '22 at 01:14
2

Because I dev on windows and deploy on linux I have created this powershell function. The solutions above where not appropriate because the binary file I had to restore. The knowledge of the bash-script is borrowed from: How to invoke bash, run commands inside the new shell, and then give control back to user?

$globalOS = "linux" #windows #linux

function ExecuteCommand($command) {
    if($command -like '*<*') {
        #Workaround for < in Powershell. That is reserved 'for future use'
        if ($globalOS -eq "windows") {
            & cmd.exe /c $command
        } else {
            $wrappercommand = "''" + $command + " ; bash''"
            & bash -c $wrappercommand
        }
    } else {
        Invoke-Expression $command
    }
}

$command = "docker exec -i mydockerdb pg_restore -U postgres -v -d mydatabase < download.dump"
ExecuteCommand($command)

1

If you want to run this command more times, you can just make a *.bat file with the original syntax. That's another solution.

seniorpreacher
  • 666
  • 2
  • 11
  • 31
  • Dont use powershell, you can use cmd prompt. In windows go to respective folder and type cmd in address bar to open the cmd prompt and there < signs can be used. – Braham Dev Yadav Aug 22 '23 at 18:14