variable comparison in powershell

In my attempts to work powershell into some of my automation scripting I've encountered something I can't make sense of. Here's the scenario. I have two command prompt windows open on a Windows 8.1 machine. Both are pointed to the same directory. In one window I launch the following powershell script:

do {
$request = get-content relayrequest.txt
echo $request
} until ($request -contains '1')

The text file initially contains 0, so the loop begins printing a column of 0's on the screen.  Meanwhile from the other command prompt window I type:

echo 1 >relayrequest.txt

and press enter.  The DO loop immediately reflects the change and begins printing a column of 1's on the screen, but does NOT end the loop.  Why isn't this variable comparison working.  I've tried it various ways and can't get the comparisons to work.  What am I missing?

February 7th, 2015 12:31pm

You're using the wrong comparison operator. Use -eq instead, as -contains doesn't work the way you think it does. Read the help file _about_Comparison_Operators and The Big Book of PowerShell Gotchas on PowerShell.Org. The comparison operator -Contains looks for a single value in a collection of values.

_about_Comparison_Operators: https://technet.microsoft.com/en-us/library/hh847759.aspx
The Big Book of PowerShell Gotchas: http://powershell.org/wp/ebooks/
Edit: Modified word choice.

Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 12:51pm

"-contains" works just fine for me.

What version of PowerShell.

Note that -contains works on any type and will search an array.  The results of Get-Content is an array.

V1 of PowerShell had some issues.

February 7th, 2015 1:09pm

"-contains" works just fine for me.

What version of PowerShell.

Note that -contains works on any type and will search an array.  The results of Get-Content is an array.

V1 of PowerShell had some issues.

Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 1:32pm

The result of Get-Content is an array if there is more than one line in the file.  But th
February 7th, 2015 1:56pm

The result of Get-Content is an array if there is more than one line in the file.  But th
Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 2:35pm

Doesn't matter:

PS C:\scripts> echo 1 > file.txt
PS C:\scripts> $x=Get-Content file.txt
PS C:\scripts> $x -contains '1'
True

February 7th, 2015 2:39pm

It dooesn't fly:

PS C:\scripts> echo 123 > file.txt
PS C:\scripts> $x=Get-Content file.txt
PS C:\scripts> $x -contains '1'
False

Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 2:40pm

PS C:\scripts> echo 'the quick brown fox is dead' > file.txt
PS C:\scripts> $x=Get-Content file.txt
PS C:\scripts> $x -contains '1'
False

Unless you are on PowerShell V1

February 7th, 2015 2:41pm

Doesn't matter:

PS C:\scripts> echo 1 > file.txt
PS C:\scripts> $x=Get-Content file.txt
PS C:\scripts> $x -contains '1'
True

Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 2:44pm

The result of Get-Content is an array if there is more than one line in the file.  But th
February 7th, 2015 2:48pm

I don't know why I've not been able to make -contains or -eq work in my system.  However, -match and -notmatch are working for me.  So I'll switch to another question.  If I have a text file that contains the the file name "sound.wav" as a string and I want to have powershell launch the executable sounder.exe with that extracted string as its target or parameter, how do I structure that command? I need to be able to change the file name contained in the text file and have powershell play whatever file I call for using sounder.exe.
Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 3:11pm

I don't know why I've not been able to make -contains or -eq work in my system.  However, -match and -notmatch are working for me.  So I'll switch to another question.  If I have a text file that contains the the file name "sound.wav" as a string and I want to have powershell launch the executable sounder.exe with that extracted string as its target or parameter, how do I structure that command? I need to be able to change the file name contained in the text file and have powershell play whatever file I call for using sounder.exe.

No. No. No. No.

Mission creep not allowed.  New question means start a new topic.  This new question has no relation to the current topic.

February 7th, 2015 3:16pm

Apologies.
Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 3:16pm

Apologies.

Not a problem.  It is just not good for other searching forums for help by topic.

February 7th, 2015 3:18pm

You're using the wrong comparison operator. Use -eq instead, as -contains doesn't work the way you think it does. Read the help file _about_Comparison_Operators and The Big Book of PowerShell Gotchas on PowerShell.Org. The comparison operator -Contains looks for a single value in a collection of values.

_about_Comparison_Operators: https://technet.microsoft.com/en-us/library/hh847759.aspx
The Big Book of PowerShell Gotchas: http://powershell.org/wp/ebooks/
Edit: Modified word choice.

  • Edited by tommymaynard Saturday, February 07, 2015 6:13 PM
Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 8:46pm

I don't know why I've not been able to make -contains or -eq work in my system.  However, -match and -notmatch are working for me.  So I'll switch to another question.  If I have a text file that contains the the file name "sound.wav" as a string and I want to have powershell launch the executable sounder.exe with that extracted string as its target or parameter, how do I structure that command? I need to be able to change the file name contained in the text file and have powershell play whatever file I call for using sounder.exe.
  • Marked as answer by Chalk-X CPG 20 hours 43 minutes ago
February 7th, 2015 11:06pm

I don't know why I've not been able to make -contains or -eq work in my system.  However, -match and -notmatch are working for me.  So I'll switch to another question.  If I have a text file that contains the the file name "sound.wav" as a string and I want to have powershell launch the executable sounder.exe with that extracted string as its target or parameter, how do I structure that command? I need to be able to change the file name contained in the text file and have powershell play whatever file I call for using sounder.exe.
  • Marked as answer by Chalk-X CPG Sunday, February 08, 2015 2:48 PM
Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 11:06pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics