title: PowerShell Running Select-String on Variable vs on File
body: <p>I'm trying to use a PowerShell script to extract my local network IP address from the output of ipconfig. I have gotten it working using a temporary output file but would like to eliminate that step and just use a variable inside the script. I cannot figure out why my script that isn't using a temporary output file won't work. Any help would be much appreciated. </p>
  
  <p>Here are the two scripts. First, the working version:</p>
  
  <pre><code>$input_path = 'C:\Users\Will\Code\Scripts\tmp.txt'
  $regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
  
  ipconfig | select-string "IPv4" | select-string -notmatch "192.168.56." &gt; tmp.txt
  
  $ipAddress = select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
  
  # Pop up an alert window with the IP address
  $wshell = New-Object -ComObject Wscript.Shell
  $wshell.Popup($ipAddress,0,"Done",0x1)
  
  # Copy the IP address to the clipboard
  $ipAddress | CLIP
  </code></pre>
  
  <p>And now the almost identical, but non-working version using only variables:</p>
  
  <pre><code>$regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
  
  $input = ipconfig | select-string "IPv4" | select-string -notmatch "192.168.56."
  
  #$input &gt; inputVar.txt
  
  $ipAddress = select-string -inputObject $input -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
  
  # Pop up an alert window with the IP address
  $wshell = New-Object -ComObject Wscript.Shell
  $wshell.Popup($ipAddress,0,"Done",0x1)
  
  # Copy the IP address to the clipboard
  $ipAddress | CLIP
  </code></pre>
  
  <p>I am using select-string to find the lines containing 'IPv4' piped into another call to select-string to eliminate the line containing the IP address of my virtual machine. If I enable the test output on line 6 of the non-working script and compare it to the 'tmp.txt' file from the working script, I can see that up to this point, both scripts work identically.</p>
  
  <p>Furthermore, lines 10-15 in both scripts are identical. So I believe that the only line in question is #8.</p>
  
  <p>I have tried it every way I can think of, but always get the same result ("IPv4" rather than the IP address) The first script works as expected and gives me the IP. </p>
  
  <p>Both of these options seem like they should work (and are probably the most logical of all that I've tried), but neither gives me the IP:</p>
  
  <pre><code>$ipAddress = select-string -inputObject $input -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>$ipAddress = $input | select-string -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
  </code></pre>
  
  <p>I've even tried eliminating all the extra steps and just making one long pipe like this: </p>
  
  <pre><code>$ipAddress = ipconfig | select-string "IPv4" | select-string -notmatch "192.168.56." | select-string -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
  </code></pre>
  
  <p>and just copying to the clipboard like this:</p>
  
  <pre><code>ipconfig | select-string "IPv4" | select-string -notmatch "192.168.56." | select-string -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } | CLIP
  </code></pre>
  
  <p>But nothing seems to work.</p>
  
  <p>Where have I gone wrong with this?</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p><strong>Updates 03/13/2015 6:30pm</strong></p>
  
  <p>I'm running Windows 7 64bit</p>
  
  <p>Here's the output of $PSVersionTable:</p>
  
  <p>Name                           Value<br>
  ----                           -----<br>
  CLRVersion                     2.0.50727.5485<br>
  BuildVersion                   6.1.7601.17514<br>
  PSVersion                      2.0<br>
  WSManStackVersion              2.0<br>
  PSCompatibleVersions           {1.0, 2.0}<br>
  SerializationVersion           1.1.0.1<br>
  PSRemotingProtocolVersion      2.1          </p>
  
  <p>And yes, my end goal is just to get the IP. Really, I just wanted to find a quick way to do that. I guess I didn't technically need the IP by itself for what I'm using it for, but then once I got into it, I got pretty caught up in trying to figure the rest of this out just out of curiosity.</p>
  
  <p>Thanks for all the help! </p>
  
  <p>I'm just getting back into coding after a ~10 year break and I'm super rusty. </p>
  
  <p>I'm not sure why my non working version isn't working for me if it's working for you. When I run it, I get "IPv4" in the pop up and on the clipboard, rather than the IP (which I get in the working version that uses the temp file.</p>
  
  <p>I'm going to integrate these regex changes to my current script and play around with your gwmi solution, <a href="https://stackoverflow.com/users/3829407/matt">Matt</a>. An initial run isn't returning anything for me, but I'll toy around with it and let you know. </p>
  
  <p>Let me know if you have any more ideas on why the version with the variable isn't working correctly on my end in light of this updated info. It's totally baffling to me. </p>
  
  <p>Thanks again!</p>
  
  <hr>
  
  <p><strong>Updates 03/13/2015 10:27pm</strong></p>
  
  <p>Here's the output of $PSVersionTable after the update:</p>
  
  <p>Name                           Value<br>
  ----                           -----<br>
  PSVersion                      4.0<br>
  WSManStackVersion              3.0<br>
  SerializationVersion           1.1.0.1<br>
  CLRVersion                     4.0.30319.18444<br>
  BuildVersion                   6.3.9600.16406<br>
  PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}<br>
  PSRemotingProtocolVersion      2.2             </p>
  
  <hr>
  
  <p>Here is what happens if I run the Piped Commands piece by piece in PowerShell. As you can see, it seems to break on line 14 at:
      $ipAddress = $ipAddress | % { $_.Matches } </p>
  
  <p>Anyhow, thought this might help:</p>
  
  <pre><code>PS C:\Users\Will\Code&gt; $regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
  PS C:\Users\Will\Code&gt; $inputVar = ipconfig | select-string "IPv4" | select-string -notmatch "192\.168\.56\."
  PS C:\Users\Will\Code&gt; $inputVar
  
     IPv4 Address. . . . . . . . . . . : 192.168.0.105
  
  
  PS C:\Users\Will\Code&gt; $ipAddress = select-string -inputObject $inputVar -Pattern $regex -AllMatches
  PS C:\Users\Will\Code&gt; $ipAddress
  
     IPv4 Address. . . . . . . . . . . : 192.168.0.105
  
  
  PS C:\Users\Will\Code&gt; $ipAddress = $ipAddress | % { $_.Matches }
  PS C:\Users\Will\Code&gt; $ipAddress
  
  
  Groups   : {IPv4}
  Success  : True
  Captures : {IPv4}
  Index    : 3
  Length   : 4
  Value    : IPv4
  
  
  
  PS C:\Users\Will\Code&gt; $ipAddress = $ipAddress | % { $_.Value }
  PS C:\Users\Will\Code&gt; $ipAddress
  IPv4
  PS C:\Users\Will\Code&gt;
  </code></pre>