Powershell: Select-String -Context not working

Created on 26 Oct 2020  路  10Comments  路  Source: PowerShell/PowerShell

Steps to reproduce


"test`r`ntest12`r`ntest123`r`ntest12`r`ntest`r`n" > logfile.txt
Select-String "test123" .\logfile.txt -Context 1,1

Expected behavior

logfile.txt:2:test12
logfile.txt:3:test123
logfile.txt:4:test12

Actual behavior

logfile.txt:3:test123

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

seems to fail on v6 & 5 as well.
Issue-Question

All 10 comments

Hmm... using your command verbatim, I don't see the symptom on my W10 machine, neither in 7.0.3 nor in 7.1.0-rc.2 nor in Windows PowerShell v5.1.18362.1110

Hi Michael, Thanks for checking.
But what do you mean with that ? Do don't get the same behavior as me ?
That's strange, as I get the same behavior on every server that I manage including my Home desktop.

Yes, I don't get the same behavior as you and instead see the expected behavior, on both editions, on Windows, macOS, Ubuntu.

Could this be an output formatting problem (though I wouldn't see how)?
Do you also get different output for this?

PS> (Select-String "test123" .\logfile.txt -Context 1,1) | select Line, { $_.Context.PreContext, $_.Context.PostContext }

Line     $_.Context.PreContext, $_.Context.PostContext 
----    -----------------------------------------------
test123 {test12, test12}

I do get the same result as you on this.
I've reinstalled my laptop last week and before that I had the same problem as well.
The only thing that can be differently then are the regional settings, but I can't imagine that his could have an impact on this behavior.

PS C:\temp> (Select-String "test123" .\logfile.txt -Context 1,1) | select Line, { $_.Context.PreContext, $_.Context.PostContext }


Line     $_.Context.PreContext, $_.Context.PostContext
----    -----------------------------------------------
test123 {test12, test12}

PS C:\temp>

Interesting; what are your regional settings? What does Get-Culture output?

I have a sneaking suspicion that the comma is being read as something else when on a different culture... Let me go check the code here.

-context 1 isn't working either. ( without the comma ).

PS C:\temp> Get-Culture

LCID             Name             DisplayName
----             ----             -----------
8192             en-BE            English (Belgium)

Hmm... nope, it _shouldn't_ be different. It's read as int[] which should be fine. If that argument parsing was causing problems, I'd expect you'd have a lot more issues.

I'm not familiar enough with this code to see what else it might be. The context tracking code is complicated as all heck. If for some reason it's reproducible with that specific culture setting, someone with more familiarity with the code might be able to recognise what's going on with some debugging, but that's probably too complicated for me to easily sort out what's happening there at any given potin.

Yes, 1,1 is two-element array 1, 1, irrespective of culture - number literals are always culture-invariant.

@moyue83, I tried with the culture temporarily set to en-BE, but still don't see the problem.
If you try with en-US, does it work then?

Thanks guys for checking this with me.
The problem happens not in Powershell but in the module PScolor that is loaded.
For reference: Issue Module

regards !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manofspirit picture manofspirit  路  3Comments

HumanEquivalentUnit picture HumanEquivalentUnit  路  3Comments

JohnLBevan picture JohnLBevan  路  3Comments

andschwa picture andschwa  路  3Comments

garegin16 picture garegin16  路  3Comments