1

Why is this coming up empty? I'm looking for variables related to the home directory. Do I have to resort to findstr in these cases? I realize that I'm piping in an object. I'd like to be able to search all properties. -InputOjbect is type PSObject.

dir variable: | select-string users

Expected output:

$                              users
HOME                           C:\Users\js
PROFILE                        C:\Users\js\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
PWD                            C:\Users\js

EDIT:

Here's a semi-weird answer, but I have to put in the properties I want.

dir variable: | select name,value | select-string users

@{Name=HOME; Value=C:\Users\js}
@{Name=PROFILE; Value=C:\Users\js\Documents\PowerShell\Microsoft.PowerShell_profile.ps1}
@{Name=PWD; Value=C:\Users\js}
js2010
  • 23,033
  • 6
  • 64
  • 66
  • 2
    I'm afraid you have a misconception about the function of `Select-String`. Try `Get-ChildItem Variable: | Where-Object {$_.Value -match 'users'}` – Olaf Oct 20 '19 at 16:43
  • Get-ChildItem does not show up $. This does: `dir variable: | where {$_.Value -match "users"}` – sunilvijendra Oct 20 '19 at 19:27
  • @sunilvijendra What do you mean with "does not show up $"? Did you try the code? BTW: `dir` is just an alias for `Get-ChildItem` ... you know that, right? ;-) – Olaf Oct 20 '19 at 21:51
  • Good point, @Olaf, though sometimes doing a string-based search in the for-display output representations of the input objects is a convenient shortcut that doesn't require knowing the objects' structure - see [this feature suggestion on GitHub](https://github.com/PowerShell/PowerShell/issues/10726). – mklement0 Oct 21 '19 at 04:24
  • The $ is just the last command argument. It's not that important. – js2010 Oct 21 '19 at 14:59
  • [This GitHub feature request](https://github.com/PowerShell/PowerShell/issues/10726) asks that `Select-String` with non-string input apply `Out-String -Stream` implicitly: – mklement0 Dec 14 '19 at 12:32

1 Answers1

2

If you want it to work like some linux shell, I would use the following:

    dir variable:  |Out-String -Stream  |Select-String "users"

Out-string converts the object into String. (-stream:line by line)

So Select-string works like intended.

Or if we want to have objects, and not a string as result:

dir variable:  |where {$_.value -match "users" -or $_.name -match "users"}
Gyula Kokas
  • 141
  • 6