Title: Make Select-String more intelligent for non-primitive types · Issue #10726 · PowerShell/PowerShell · GitHub
Open Graph Title: Make Select-String more intelligent for non-primitive types · Issue #10726 · PowerShell/PowerShell
X Title: Make Select-String more intelligent for non-primitive types · Issue #10726 · PowerShell/PowerShell
Description: Summary of the new feature/enhancement As a user, I want to be able to easily use Select-String to find string data in formatted output, so that I can find and process data in PowerShell more quickly. By default, when you pass non-string...
Open Graph Description: Summary of the new feature/enhancement As a user, I want to be able to easily use Select-String to find string data in formatted output, so that I can find and process data in PowerShell more quick...
X Description: Summary of the new feature/enhancement As a user, I want to be able to easily use Select-String to find string data in formatted output, so that I can find and process data in PowerShell more quick...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/10726
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Make Select-String more intelligent for non-primitive types","articleBody":"# Summary of the new feature/enhancement\r\n\r\nAs a user,\r\nI want to be able to easily use `Select-String` to find string data in formatted output,\r\nso that I can find and process data in PowerShell more quickly.\r\n\r\nBy default, when you pass non-string data into the `Select-String` cmdlet, it will find matches based on the object's `ToString()` method results. This is great when the `ToString` output actually represents the data you want to match, but very often `ToString` results do not represent the data that you see in PowerShell, and this can result in incorrect or failed matches.\r\n\r\nFor example, consider this script:\r\n\r\n```powershell\r\n# First, capture the date:\r\n$date = Get-Date\r\n\r\n# Now, get the current month in string format:\r\n$month = $date.ToString(\"MMMM\")\r\n\r\n# Now, look at how the date renders in PowerShell, showing the month as a string\r\n$date\r\n\r\n# Now, try to match the month using Select-String. This returns nothing.\r\nGet-Date | Select-String -Pattern $month\r\n```\r\n\r\nThat script shows the current date, including the month in string format, but if you try to select that string based on the actual string month, there are no matches. Why does this happen? Because `ToString()` on `DateTime` objects returns the date time with a numeric month, not the string month.\r\n\r\nNow let's look at a more realistic example:\r\n\r\n```powershell\r\n# Get some services, including the Windows Update service, and filter output on the\r\n# string \"Update\"\r\nGet-Service wuauserv,bits | Select-String Update\r\n```\r\n\r\nThat returns nothing. Why? Because the `ToString` method on service objects returns the name of service, so you can't filter output based on a partial match of a display name string this way.\r\n\r\nHere is one more example:\r\n\r\n```powershell\r\nGet-Process -Id $PID | Format-List * | Select-String Memory\r\n```\r\n\r\nThis also returns nothing, because of the `ToString` method on `Format` cmdlet output objects returns their type name, none of which match `Memory`.\r\n\r\n_It is reasonable for a user to expect to be able to easily and consistently parse/filter output that is rendered in the PowerShell console, but this is not possible unless they pipe to `Out-String -Stream` before they then pipe that streamed result to `Select-String`._\r\n\r\n# Proposed technical implementation details (optional)\r\n\r\n[EDIT]: Updated to reflect the parameter name we decided to move forward in the discussion below.\r\n\r\nI want to make `Select-String` better by adding a new ~~`-ConsoleOutput` switch parameter (or some better parameter name: suggestions welcome)~~ `-FromFormattedOutput` switch parameter that indicates that you want to select a string based on the console output of the data that is piped into `Select-String`, which would automatically take care of the formatting and output of non-value and non-string types (and non-MatchInfo, but that's a special case internally for `Select-String`), and select string matches based on that output rather than based on the `ToString` method output of individual objects.\r\n\r\n# Additional details\r\n\r\nPersonally I would prefer if Select-String worked this way by default for non-value and non-string types, but that would be a breaking change at this point, so that's not an option; however, users who want it to work this way by default can use `$PSDefaultParameterValues['Select-String:FromFormattedOutput'] = $true`, and that will have the same result.\r\n","author":{"url":"https://github.com/KirkMunro","@type":"Person","name":"KirkMunro"},"datePublished":"2019-10-07T23:57:34Z","url":"https://github.com/PowerShell/PowerShell/issues/10726"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5eb099ba-c8f2-339f-af67-bf9eb72d83d3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E6EA:1790B5:1F065C:29C647:6AFD448D |
| html-safe-nonce | a879af998c06e5fd04b568b450bbf2f32372b479a019273c3980e119477ac848 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNkVBOjE3OTBCNToxRjA2NUM6MjlDNjQ3OjZBRkQ0NDhEIiwidmlzaXRvcl9pZCI6Ijg1MTQ5ODM2NTIwODg0MzE3NTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0efb46590af13b551cd3c07eb43b18a0c766c83f6f857d214bb18c5d4d031afb |
| hovercard-subject-tag | issue:503750984 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/10726/issue_layout |
| twitter:image | https://opengraph.githubassets.com/063883ac7c5cce3da8eed8f6c97699047fcef96660e0a5d944327ba6e7d14009/PowerShell/PowerShell/issues/10726 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/063883ac7c5cce3da8eed8f6c97699047fcef96660e0a5d944327ba6e7d14009/PowerShell/PowerShell/issues/10726 |
| og:image:alt | Summary of the new feature/enhancement As a user, I want to be able to easily use Select-String to find string data in formatted output, so that I can find and process data in PowerShell more quick... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | KirkMunro |
| hostname | github.com |
| expected-hostname | github.com |
| None | bdbacc25b921224ff0befcb9e629560a1255be51aec9991d6e3f95b5e9151474 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 556dd37bb0aceffb25724e151b5d858b3265b40b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width