Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - read from standard input (stdin)

Reply
 
Old 07-30-2007   #1 (permalink)
tiefseeruebe


 
 

read from standard input (stdin)

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

My System SpecsSystem Spec
Old 07-30-2007   #2 (permalink)
y.bobzhang@gmail.com


 
 

Re: read from standard input (stdin)

On Jul 30, 2:52 pm, tiefseeruebe
<tiefseeru...@discussions.microsoft.com> wrote:
> 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


try read-host.

My System SpecsSystem Spec
Old 07-30-2007   #3 (permalink)
y.bobzhang@gmail.com


 
 

Re: read from standard input (stdin)

On Jul 30, 3:14 pm, y.bobzh...@gmail.com wrote:
> On Jul 30, 2:52 pm, tiefseeruebe
>
>
>
>
>
> <tiefseeru...@discussions.microsoft.com> wrote:
> > 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

>
> try read-host.- Hide quoted text -
>
> - Show quoted text -


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

My System SpecsSystem Spec
Old 07-30-2007   #4 (permalink)
Shay Levi


 
 

Re: read from standard input (stdin)

Input redirection is not implemented in PowerShell v1.0.


Shay
http://scriptolog.blogspot.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.
>
> Can someone help me please.
>
> Tanks and Regards
>
> tiefseeruebe
>



My System SpecsSystem Spec
Old 07-30-2007   #5 (permalink)
Jacques Barathon [MS]


 
 

Re: read from standard input (stdin)

"tiefseeruebe" <tiefseeruebe@discussions.microsoft.com> wrote in message
news:39D8DA88-DED4-4AC8-BF42-C0F1E0ACF6B5@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

My System SpecsSystem Spec
Old 07-30-2007   #6 (permalink)
dreeschkind


 
 

RE: read from standard input (stdin)

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" wrote:

> 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

My System SpecsSystem Spec
Old 08-02-2007   #7 (permalink)
tiefseeruebe


 
 

RE: read from standard input (stdin)

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" wrote:

> 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" wrote:
>
> > 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

My System SpecsSystem Spec
Old 08-02-2007   #8 (permalink)
dreeschkind


 
 

RE: read from standard input (stdin)

"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
My System SpecsSystem Spec
Old 08-03-2007   #9 (permalink)
tiefseeruebe


 
 

RE: read from standard input (stdin)

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

"dreeschkind" wrote:

> "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

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
VbSystemModal type functionality for Read-Host input PowerShell
Read console input in Do/While Loop. PowerShell
PowerShell: how to read Pipe.Input from a script? PowerShell
PowerShell: how to read Pipe.Input from a script? PowerShell
How to read CSV to another command's input? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46