If you are reading this you must have used PowerShell Select-object in PowerShell to select properties of an object something like
Or, Where-Object cmdlet to filter out values on any specific object property like
All these work well when you know the Property Name you are searching.
But what if you don’t know the property name and you’re sure that there is a value against some property which you’re searching.
For example, I know in the object of WMI class Win32_OperatingSystem there is a Property-value pair for Windows operating system name, but what if I don’t know the exact Property name against which this value may be present, then how I’d get the value?
The normal approach is to list all the properties and values like in the following
Get-WmiObject win32_OperatingSystem | Select-Object *
Or,
Get-WmiObject win32_OperatingSystem | Format-List *
and then manually scan through the result until you find the Property-Value pair you were looking, so that you can use the property name to get the value next time.
I’ve done this often and I’m sure you’ve experienced this as well, but things get tough when there are more than 100 properties in an object for example in an AD object.
I think there has to be a smarter way to search the values even though, we don’t know the name of the property.
Hence I wrote this quick function Select-Value , which has following capabilities –
- You can search a value directly in object properties, without being aware of the property name, the function does all the searching for you.
- It adds the property of the matching value to the default display properties so that you can see Property-Value pair on the console.
- Maintains the Object type, and properties so that you can further use it in the pipeline or perform a set of operations.
- Compared to other filtering methods the function performs well, without any significant difference in execution time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Select-Value{ | |
[Cmdletbinding()] | |
Param( | |
[parameter(Mandatory=$true)] [String] $Value, | |
[parameter(ValueFromPipeline=$true)] $InputObject | |
) | |
Process{ | |
# Identify the PropertyName for respective matching Value, in order to populate it Default Properties | |
$Property = ($PSItem.properties.Where({$_.Value -Like "$Value"})).Name | |
If($Property){ | |
# Create Property a set which includes the 'DefaultPropertySet' and Property for the respective 'Value' matched | |
$DefaultPropertySet = $PSItem.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames | |
$TypeName = ($PSItem.PSTypenames)[0] | |
Get-TypeData $TypeName |Remove-TypeData | |
Update-TypeData -TypeName $TypeName -DefaultDisplayPropertySet ($DefaultPropertySet+$Property |Select-Object -Unique) | |
# Output Objects | |
$PSItem | Where-Object {$_.properties.Value -like "$value"} | |
} | |
} | |
} |
Add this quick function to your profile and share your feedback.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Content $profile -Value (iwr https://goo.gl/91J8vS); . $Profile |
Hoping you’ll find it useful, cheers!
Prateek Singh
Related posts
9 Comments
Leave a ReplyCancel reply
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,132,876 People Reached
[…] on June 25, 2017 submitted by /u/Prateeksingh1590 [link] [comments] Leave a […]
Why doesn’t this work?
Get-OwaMailboxPolicy | Select-Value “True”
Nice script, but couldnt the -Query parameter be used in place of a Where-Object for the Get-WmiObject cmdlet?
It’s not working for example with Get-ChildItem or Get-Process.
The $PSItem’s have no properties property.
Is not working with Get-ADUser
What I was using for this, is this function I made:
function Get-Values{
Get-Values “@domain.com”
Type or paste the command: Get-ADUser user -Properties *
EmailAddress : user@domain.com
mail : user@domain.com
This example searches the term “@user.com” in all properties of a command.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$Search
)
$Command = Read-Host “Type or paste the command”
$Command = Invoke-Expression $Command
$PropretiesOfTheCommand = $Command | Get-Member -MemberType Properties | select -ExpandProperty Name
$Properties = foreach ($Property in $PropretiesOfTheCommand){
$Property | Where {$Command.$Property -like “*$Search*”}
}
if ($Property -ne $Null){
$Property -join(“,”) | Out-Null
$Command | select $Properties | Format-List
} else{
Write-Host
Write-Warning “No properties found with that value”
Write-Host
}
}
Oh… The code didn’t paste well… The description is incomplete. But the command part of the function should work normally.
The reason why this isn’t working for most of you is that this function only works with WMI Objects, because WMI Objects include a property called “properties’ which enumerates all the properties and their values. That is what the function is using. Other types of objects do not include that.
[…] + Read More […]