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 - $Variable Is In {1,2,3,4}

Reply
 
Old 10-23-2008   #1 (permalink)
C S S


 
 

$Variable Is In {1,2,3,4}

I'd like to read from the command line some user input, and then qualify that
they entered a valid response (e.g. "A", "B", or "1"). Is there a
conditional expression that can do this?

Also, I've been searching through Google and MS for about 30 min now looking
for, on the Microsoft site preferably, a comprehensive powershell reference
(where I would have liked to have been able to find the answer to the above
question). I found blogs, script examples, books.... but not just a simple
repository that was comprehensive. Does one exist and can someone give me a
link?

Thank you

My System SpecsSystem Spec
Old 10-23-2008   #2 (permalink)
PaulChavez


 
 

RE: $Variable Is In {1,2,3,4}

$userinput = write-host "Enter something"
if ($userinput -eq "a") { "Valid" } else {"invalid" }


if you want to match more complex strings you can use -match and a regex.

Match a, b or 1 case insensitive:
if ($userinput -match "[a,b,1]")


"C S S" wrote:
Quote:

> I'd like to read from the command line some user input, and then qualify that
> they entered a valid response (e.g. "A", "B", or "1"). Is there a
> conditional expression that can do this?
>
> Also, I've been searching through Google and MS for about 30 min now looking
> for, on the Microsoft site preferably, a comprehensive powershell reference
> (where I would have liked to have been able to find the answer to the above
> question). I found blogs, script examples, books.... but not just a simple
> repository that was comprehensive. Does one exist and can someone give me a
> link?
>
> Thank you
My System SpecsSystem Spec
Old 10-23-2008   #3 (permalink)
RickB


 
 

Re: $Variable Is In {1,2,3,4}

On Oct 23, 11:09*am, C S S <C...@xxxxxx> wrote:
Quote:

> I'd like to read from the command line some user input, and then qualify that
> they entered a valid response (e.g. "A", "B", or "1"). *Is there a
> conditional expression that can do this?
>
> Also, I've been searching through Google and MS for about 30 min now looking
> for, on the Microsoft site preferably, a comprehensive powershell reference
> (where I would have liked to have been able to find the answer to the above
> question). * I found blogs, script examples, books.... but not just a simple
> repository that was comprehensive. *Does one exist and can someone giveme a
> link?
>
> Thank you
if ("A", "B","1" -contains $UserInput)

The -contains operator might work backward from the way you usually
check input but it is definitely the way to go in cases like this.
You can add ,'','exit' for example and now an empty string or the word
exit are allowed very simply.
My System SpecsSystem Spec
Old 10-23-2008   #4 (permalink)
RickB


 
 

Re: $Variable Is In {1,2,3,4}

On Oct 23, 12:26*pm, RickB <rbiel...@xxxxxx> wrote:
Quote:

> On Oct 23, 11:09*am, C S S <C...@xxxxxx> wrote:
>
Quote:

> > I'd like to read from the command line some user input, and then qualify that
> > they entered a valid response (e.g. "A", "B", or "1"). *Is there a
> > conditional expression that can do this?
>
Quote:

> > Also, I've been searching through Google and MS for about 30 min now looking
> > for, on the Microsoft site preferably, a comprehensive powershell reference
> > (where I would have liked to have been able to find the answer to the above
> > question). * I found blogs, script examples, books.... but not just asimple
> > repository that was comprehensive. *Does one exist and can someone give me a
> > link?
>
Quote:

> > Thank you
>
> if ("A", "B","1" -contains $UserInput)
>
> The -contains operator might work backward from the way you usually
> check input but it is definitely the way to go in cases like this.
> You can add ,'','exit' for example and now an empty string or the word
> exit are allowed very simply.
Another handy trick to using this method is:

1..15,'exit' -contains $input

Here any number from 1 thru 15 or 'exit' is allowed.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Ren file with variable PowerShell
Import-CSV from Web/Variable PowerShell
Set-Variable PowerShell
tab and variable PowerShell
How can I ensure that a variable is a built-in powershell variable? 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