Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

read from standard input (stdin)

1,552 views
Skip to first unread message

tiefseeruebe

unread,
Jul 30, 2007, 2:52:03 AM7/30/07
to
Hi,

I want to write a power shell script that reads from the standard input. I
try to call my script stored in test.ps1 like this:

>powershell -command .\test.ps1 < .\test.txt

or like this:

>echo foobar | powershell -command .\test.ps1

I just want to access the content of stin. But I don't know how to do this.
Reading by Read-Host only waits for keyboard input.

Can someone help me please.

Tanks and Regards

tiefseeruebe

y.bob...@gmail.com

unread,
Jul 30, 2007, 3:14:56 AM7/30/07
to
On Jul 30, 2:52 pm, tiefseeruebe

try read-host.

y.bob...@gmail.com

unread,
Jul 30, 2007, 3:20:22 AM7/30/07
to
> try read-host.- Hide quoted text -
>
> - Show quoted text -

sorry, I didn't read the last line. Ignore my reply.

Shay Levi

unread,
Jul 30, 2007, 3:42:12 AM7/30/07
to
Input redirection is not implemented in PowerShell v1.0.


Shay
http://scriptolog.blogspot.com

Jacques Barathon [MS]

unread,
Jul 30, 2007, 5:22:33 AM7/30/07
to
"tiefseeruebe" <tiefse...@discussions.microsoft.com> wrote in message
news:39D8DA88-DED4-4AC8...@microsoft.com...

> Hi,
>
> I want to write a power shell script that reads from the standard input. I
> try to call my script stored in test.ps1 like this:
>
>>powershell -command .\test.ps1 < .\test.txt
>
> or like this:
>
>>echo foobar | powershell -command .\test.ps1
>
> I just want to access the content of stin. But I don't know how to do
> this.
> Reading by Read-Host only waits for keyboard input.

Input redirection with "<" is not supported in PowerShell. However you can
easily read the content of a file and pass it to the next command in the
pipeline like this:

get-content test.txt | test.ps1

If you want to kick off the whole process from a CMD session, you should be
able to do it like this:

powershell -command "& {get-content c:\test\test.txt | c:\test\test.ps1}"

Note that you will probably have to use absolute paths for the text file as
well as for the script (unless it is in the PATH).

Jacques

dreeschkind

unread,
Jul 30, 2007, 2:38:01 PM7/30/07
to
As others have mentioned, the '<' operator is not supported in PowerShell
v1.0. It is reserved for future use.
However, your second shot is actually the way to go in version 1.
Note that when invoking the command you need to be aware of the differences
between the various command shells that you might be using. For example
quoting, escaping, variable interpolation etc. works quite different in
PowerShell, cmd.exe and bash.

### PowerShell test script
PS C:\> get-content C:\write-input.ps1
$input | foreach {write-host $_}

### example for cmd.exe
C:\>echo bratwurst, banane | powershell -noprofile -command "$input | .
C:\write-input.ps1"
bratwurst, banane

### example for PowerShell
PS C:\> echo bratwurst, banane| powershell -noprofile -command '$input | .
C:\write-input.ps1'
bratwurst
banane

Also be aware of the different ways the comma is interpreted in each command
shell.

--
greetings
dreeschkind

tiefseeruebe

unread,
Aug 2, 2007, 5:54:03 PM8/2/07
to
Thanks for the responses. The point seems to be the parameter -noprofile.
With out that it does not work as expected. However I don't understand that.

Regards tiefseeruebe

dreeschkind

unread,
Aug 2, 2007, 7:12:02 PM8/2/07
to
"tiefseeruebe" wrote:

> Thanks for the responses. The point seems to be the parameter -noprofile.
> With out that it does not work as expected. However I don't understand that.

Can you be a little bit more specific when you say "does not work as
expected"?
Do you get any error messages? The parameter -noprofile will just tell
PowerShell not to load your profile at startup ($profile). It should work
without the parameter as well.

--
greetings
dreeschkind

tiefseeruebe

unread,
Aug 3, 2007, 5:50:01 PM8/3/07
to
Strange: you are right. I was sure that it didn't work w/o that -noprofile.
However now, as i try, there is no difference. I don't remember what I did,
it was late already. Thanks anyway.

regards
tiefseeruebe

0 new messages