question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Select-String` should have a `-AsPSObject` switch returning named groups as properties of an object

See original GitHub issue

Summary of the new feature / enhancement

Currently, if you run something like:

PS /Users/steve/repos/PowerShell> ls -l *.md | select-string "(?<mode>.*?)\s+(?<hardlinks>[0-9]+)\s+(?<owner>\w+)\s+(?<group>\w+)\s+(?<size>[0-9]+)\s+(?<date>\w+\s[0-9]+\s[0-9:]+)\s+(?<filename>.+)"

-rw-r--r--  1 steve  staff   5122 Oct 24 08:37 ADOPTERS.md
-rw-r--r--  1 steve  staff  20383 Oct 27 09:33 README.md

select-string just highlights the matches (imagine the text above highlighted). However, it would be useful to have a -AsPSObject switch so that if there are named groups, those becomes properties of an object instance:

PS /Users/steve/repos/PowerShell> ls -l *.md | select-string "(?<mode>.*?)\s+(?<hardlinks>[0-9]+)\s+(?<owner>\w+)\s+(?<group>\w+)\s+(?<size>[0-9]+)\s+(?<date>\w+\s[0-9]+\s[0-9:]+)\s+(?<filename>.+)"

mode       hardlinks owner group size  date         filename
----       --------- ----- ----- ----  ----         --------
-rw-r--r-- 1         steve staff 20383 Oct 27 09:33 README.md
-rw-r--r-- 1         steve staff 20383 Oct 27 09:33 ADOPTERS.md

Proposed technical implementation details (optional)

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jhoneillcommented, Nov 2, 2022

I like this idea. Someone is bound to ask “does this belong in select-string ?” and I think the answer is “yes, it does” 😃

0reactions
ImportTastecommented, May 2, 2023

Didn’t realize how many parameter sets this cmdlet has. That’ll be the hardest part of adding this feature…

In time we did last change in the cmdlet a conclusion was it better to have new cmdlet or cmdlets because of complicity of the cmdlet 😃

New-RegexSelection perhaps? Pretty much every New-* command outputs a PSObject, and the command could have more similarities to the C# Regex method beyond just the pattern, like having parameters for the various RegexOptions. You could even pair it with a New-RegexPattern command for compiled regex patterns, e.g.:

$Lines_With_A_2to4_Digit_Number = New-RegexPattern '^(.*?\b\d{2,4}\b.*?)$' -Multiline
$str1_Selection = New-RegexSelection -String $str1 -Selector $Lines_With_A_2to4_Digit_Number
$str2_Selection = New-RegexSelection -String $str2 -Selector $Lines_With_A_2to4_Digit_Number

# Equivalent but slower because a compiled selector isn't used:
$str1_Selection = New-RegexSelection -String $str1 -Pattern '^(.*?\b\d{2,4}\b.*?)$' -Multiline
$str2_Selection = New-RegexSelection -String $str2 -Pattern '^(.*?\b\d{2,4}\b.*?)$' -Multiline
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to select a substring in select/group command
Just add another calculated property to the code you have to get the array of user objects: $ADUsers = foreach ($OU in $OUs)...
Read more >
Select-Object (Microsoft.PowerShell.Utility)
The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of...
Read more >
Everything you wanted to know about PSCustomObject
PSCustomObject is a great tool to add into your PowerShell tool belt. ... Sometimes you need a list of all the property names...
Read more >
James O'Neill's Blog | Page 2
If you want the original object and its properties, they are there, untouched. They are available for a Select-Object or Format-List command to ......
Read more >
PowerShellCookbook.psm1 1.2
Store the property names and identifiers for all of the shell ## properties $itemProperties = @{} ## Get the file from the input...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found