# ============================================================================================== # # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009 # # NAME: Select-PropertyValue.ps1 # # AUTHOR: Jeffery Hicks , SAPIEN Technologies # DATE : 6/10/2009 # # COMMENT: This function can be used in a pipelined expression # to return all properties of a given object that have a defined value. # # Using an expression like: # # get-process PowerShell | select * # # Is a handy way to see all properties and their values. But sometimes # you may only want to see properties that have actual values. For # those situations, use this function. # # get-process PowerShell | Select-PropertyValue # # This if you dot source this script it will also create an # alias for the function of spv # # get-process PowerShell | spv # # The -debug parameter will turn on debug messages. Use # -NoWMiSystem to filter out WMI System properties like # __GENUS when you pipe a WMI object to the function. # DISCLAIMER AND WARNING: # THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY # KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. # TEST THOROUGHLY IN A NON-PRODUCTION ENVIRONMENT. IF YOU DON'T KNOW WHAT THIS # SCRIPT WILL DO...DO NOT RUN IT! # ============================================================================================== Filter Select-PropertyValue { Param([switch]$NoWMISystem,[switch]$debug) if ($debug) { $debugPreference="Continue" } Write-Debug "In process" if (-Not $properties) { Write-Debug "Creating property list" #get properties for the pipelined object sorted by property name $properties=$_ | Get-Member -membertype Properties | sort Name #filter out WMI System properties if -NoWMISystem if ($NoWMISystem) { Write-Debug "Filtering out WMI System properties" $properties=$properties | where {$_.name -notlike "__*"} } Write-Debug "Found $($properties.count) properties" } #create an empty custom object Write-Debug "Creating empty object" $obj=New-Object PSObject #enumerate the list of properties foreach ($property in $properties) { Write-Debug "Checking $($property.name)" #if object has a value for the current property if ($_.($property.name)) { Write-Debug "found value $($_.($property.name))" #assign properties $obj | Add-Member -MemberType Noteproperty -name $property.Name -value ($_.($property.name)) } #end If } #end ForEach #write the custom object to the pipeline write $obj } #end function Set-Alias -Name spv -value Select-PropertyValue #examples # Get-WmiObject win32_bios | spv # Get-WmiObject win32_logicaldisk -filter "drivetype=3" | spv -noWMISystem # Get-Process Powershell | spv -debug # dir c:\test\*.txt | spv | out-file data.txt