109

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?

1
  • Try to run this on normal command prompt and not on power shell. Apr 4 at 19:30

6 Answers 6

102

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
3
  • You can use the alias cat to get file content easier.
    – iTzVoko
    Jun 1, 2023 at 13:51
  • 1
    @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) Jun 1, 2023 at 15:01
  • What is the reason for this error anyways?
    – Lance
    Jan 23 at 16:53
65

Also try:

cmd /c '.\test_cfdp.exe < test.full | tee test.log'
4
  • 1
    tee is an alias for PowerShell's Tee-Object cmdlet, though, so this won't work the same/at all. Sep 20, 2022 at 1: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 5, 2022 at 11:39
  • yes - running the command in Command Prompt works fine. This error is misleading.
    – kiev
    Apr 14, 2023 at 16:13
  • did the trick, thanks Dec 10, 2023 at 15:13
8

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
1
  • Switching to cmd and using the < operator was the easiest solution in my case, thanks! Aug 10, 2023 at 9:54
4

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

2
  • 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.
    – Community Bot
    Jul 1, 2022 at 14:36
  • That has already been proposed in this earlier and much-upvoted answer. Sep 20, 2022 at 1:14
3

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)

0
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.

1
  • 1
    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. Aug 22, 2023 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.