3

After upgrading from to Windows 11 the Select-String cmdlet stopped working and everytime I use it, it tries to "open an unknown file" as in image bellow:

Example of strange Select-String behaviour

I found it out when I tried to run one of my ps scripts from batch file - in Win 10 it worked well, but in Win 11 it doesn't. When I run the code in VSCode, it works correctly, I have no clue why.

Example of VSCode's Select-String correct behaviour

I tried to copy texts from VSCode (in case of different encoding) to a new file and directly to PowerShell terminal, but none of these worked.

I tried also the new Windows 11 terminal and updating PowerShell to the latest version (7.2.2 x64), but nothing worked.

Dis/Enabling the system function of Windows PowerShell 2.0 has no effect in this case as well.

EDIT:
I found out that if I run the command Update-Script (without any parameters) at the beginning of the script, Select-String works as expected (before that the cmdlet seems to not exist at all - Get-Help Select-String didn't find anything, but after using Update-Script it finds appropriate help). Some other commands does the same (eg. Trace-Command). See the output:

PS C:\> Get-Command Select-String
CommandType  Name           Version    Source
-----------  ----           -------    ------
Application  Select-String  0.0.0.0    C:\WINDOWS\system32\Select-String

PS C:\> Update-Script
PS C:\> Get-Command Select-String

CommandType  Name           Version    Source
-----------  ----           -------    ------
Cmdlet       Select-String  3.1.0.0    Microsoft.PowerShell.Utility

The Trace-Command "makes" the Select-String works properly as the Update-Script does and gives this output:

DEBUG: CommandDiscovery Information: 0 : Looking up command: Select-String
DEBUG: CommandDiscovery Information: 0 : Cmdlet found: Select-String  Microsoft.PowerShell.Commands.SelectStringCommand 
hello everybody!

Why do I have to do that (call the Update-Script), when Select-String should work without it?

FFko
  • 77
  • 7
  • 2
    Try and run `Trace-Command -Expression {"hello everybody!" | Select-String "everybody"} -Name Command*,Location*,Path* -Option All -PSHost`. If it works it's gonna print a bunch of debug messages to the screen. Remove/redact any sensitive details (it might contain file system paths) and post it. – Mathias R. Jessen Mar 30 '22 at 21:33
  • @MathiasR.Jessen I've never used that cmdlet, so I tried your command and got back two pretty unimpressive lines (Looking up command, then the library it was found in). Would you expect something different? Or only in the case things aren't working right? – TheMadTechnician Mar 30 '22 at 22:16
  • @TheMadTechnician That's expected when working correctly, yes. My suspicion is either `Select-String` or some implicit command might have been alias'd to a file path, which might show up in trace output – Mathias R. Jessen Mar 30 '22 at 23:53
  • Open the unknown file in notepad; what's inside it? – TessellatingHeckler Mar 31 '22 at 00:24
  • @FFko Thanks, that does indeed look just as expected. Next thing to check would be your prompt function (`Get-Content function:\prompt`), or your profile scripts (check if script files exist at each of the locations listed when you run `$profile |fl -Force *host*` and whether they contain code that might try to execute/invoke/launch a file) – Mathias R. Jessen Mar 31 '22 at 12:01
  • @MathiasR.Jessen prompt function contains: `"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "; # .Link # https://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml` Not really sure, what does it say... – FFko Mar 31 '22 at 12:09
  • @MathiasR.Jessen About the profile - none of the four files (All/Current users all/Current Host[s]) exists. Creating the missing profile files didn't help. – FFko Mar 31 '22 at 13:02
  • When in doubt, sysinternals process monitor. You can filter "operation contains process" to see what's launching. Or get-command select-string. – js2010 Mar 31 '22 at 14:11
  • @FFko please edit the question and paste the `Trace-Command` output there. Don't post in comments because it's unreadable and people won't read comments anyway – phuclv Mar 31 '22 at 15:24

1 Answers1

3

Well, there are a lot of discussions (not really solved) about powershell "how do you want to open this file", but finally, this was the problem of mine:

https://serverfault.com/questions/1038546/powershell-start-job-returns-a-how-do-you-want-to-open-this-file-with-some-cm

Solution:

Delete file c:\Windows\System32\Select-string (0 bytes, no file extension). I have no idea how it got there...

FFko
  • 77
  • 7