39

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272
  • 3
    Since you gave piping a script to `mysql` as your example, I thought I should [point you to this](http://stackoverflow.com/questions/6365191/import-large-mysql-sql-file-on-windows-with-force/6365553#6365553) in case you try to use Powershell to pipe a backup. – Joel B Fant Jul 20 '11 at 14:34
  • That is exactly what I was looking for. Granted, not the best solution, but a nice hack, without having to switch back to dos-prompt. – Andriy Drozdyuk Jul 20 '11 at 19:19
  • 1
    A year and a half later, I still don't have an answer to this specific question of why input redirect was not implemented. – Joel B Fant Feb 14 '13 at 04:08

2 Answers2

12

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 8
    Redirection was covered in a Scripting Guys post using Get-Content http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/16/working-around-legacy-redirection-issues-with-powershell.aspx – Chad Miller Jul 20 '11 at 15:11
  • 5
    @Chad Miller, Oh, of course - that would be the obvious place for a user of powershell to look! /end-sarcasm. Thanks. – Andriy Drozdyuk Jul 20 '11 at 19:16
  • ss64.com is also an obvious place to look period; though granted, the answer at http://ss64.com/ps/syntax-redirection.html is only implicit – nik.shornikov Apr 29 '15 at 18:51
0

I don't think MySQL will accept PowerShell objects piped to it - as Stephan says though, you could use Get-Content and pipe it to the next command.

Check out Stack Overflow question Is PowerShell ready to replace my Cygwin shell on Windows? for reasons on why they haven't copied Unix shells.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt
  • 1,931
  • 12
  • 20
  • Did you read Jeffery Snover's comment? He talks about the reasons. – Matt Jul 20 '11 at 20:07
  • I see no reasons on input redirects in particular, and I am really not interested in Unix vs Windows tools. I was only curious about the one particular tool. After all, the output redirection ">" is implemented! – Andriy Drozdyuk Jul 22 '11 at 16:59
  • I was considering the question more generically in why certain aspects of powershell do not implement the same functions as other shells. I didn't say it answered why < wasn't implemented though it provides insight into the design process, – Matt Jul 24 '11 at 10:50