Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "<" input redirect not implemented in PowerShell?

Tags:

powershell

Why is input redirection not implemented in PowerShell?

To do something like this:

mysql -u root < create.sql 

I had to switch to "cmd.exe".

Is there an alternative way of doing this in PowerShell?

Please note that the output redirection ">" is implemented in PowerShell. Please consider this before giving an answer.

like image 458
Andriy Drozdyuk Avatar asked Jul 20 '11 13:07

Andriy Drozdyuk


People also ask

How do you achieve input output redirection?

The wc -l command returns the number of rows in a file followed by the name of the file. By default, the command takes the name of the file from the standard input. Using the ' < ' symbol, we redirect the standard input to file.

Can input be redirected?

Input Redirection Just as the output of a command can be redirected to a file, so can the input of a command be redirected from a file. As the greater-than character > is used for output redirection, the less-than character < is used to redirect the input of a command.

How do I redirect a file in PowerShell?

There are two PowerShell operators you can use to redirect output: > and >> . The > operator is equivalent to Out-File while >> is equivalent to Out-File -Append . The redirection operators have other uses like redirecting error or verbose output streams.


1 Answers

Although I'm not entirely sure that this question belongs on Stack Overflow, have you looked at the PS Cmdlet for Get-Content? Look how it's used in the examples on TechNet in Using the Get-Content Cmdlet.

Example:

Get-Content c:\scripts\test.txt | Foreach-Object {Get-Wmiobject -computername $_ win32_bios} 

Update: Above link to TechNet is broken, but mentioned in comment by Chad Miller Scripting Guy's post Working Around Legacy Redirection Issues with PowerShell gives three options: -use CMD /c, Echo, and Get-Content.

like image 132
Stephen Kiningham Avatar answered Oct 01 '22 12:10

Stephen Kiningham