Objects are filled with rich information, yet objects may contain empty properties. This is especially true for objects retrieved from Active Directory.
Here is a useful function called Remove-EmptyProperty which accepts arbitrary objects and removes all empty properties:
# Only list output fields with content function Remove-EmptyProperty { param ( [Parameter(Mandatory,ValueFromPipeline)] $InputObject, [Switch] $AsHashTable ) begin { $props = @() } process { if ($props.COunt -eq 0) { $props = $InputObject | Get-Member -MemberType *Property | Select-Object -ExpandProperty Name } $notEmpty = $props | Where-Object { !($InputObject.$_ -eq $null -or $InputObject.$_ -eq '' -or $InputObject.$_.Count -eq 0) | Sort-Object } if ($AsHashTable) { $notEmpty | ForEach-Object { $h = [Ordered]@{}} { $h.$_ = $InputObject.$_ } { $h } } else { $InputObject | Select-Object -Property $notEmpty } } }
Here is an example illustrating how to use the function:
PS> Get-Process -Id $pid Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1432 87 324452 346012 145,13 14340 1 powershell_ise PS> Get-Process -Id $pid | Remove-EmptyProperty Handles : 1256 Name : powershell_ise NPM : 85320 PM : 332496896 SI : 1 VM : 1491988480 WS : 363655168 __NounName : Process BasePriority : 8 Handle : 5776 HandleCount : 1256 Id : 14340 MachineName : . MainModule : System.Diagnostics.ProcessModule (PowerShell_ISE.exe) MainWindowHandle : 10033714 MainWindowTitle : C:\Users\tobwe MaxWorkingSet : 1413120 MinWorkingSet : 204800 Modules : {System.Diagnostics.ProcessModule (PowerShell_ISE.exe), System.Diagnostics.ProcessModule (ntdll.dll), System.Diagnostics.ProcessModule (MSCOREE.DLL), System.Diagnostics.ProcessModule (KERNEL32.dll)...} NonpagedSystemMemorySize : 85320 NonpagedSystemMemorySize64 : 85320 PagedMemorySize : 332496896 PagedMemorySize64 : 332496896 PagedSystemMemorySize : 1272400 PagedSystemMemorySize64 : 1272400 PeakPagedMemorySize : 389857280 PeakPagedMemorySize64 : 389857280 PeakVirtualMemorySize : 1601478656 PeakVirtualMemorySize64 : 1601478656 PeakWorkingSet : 423972864 PeakWorkingSet64 : 423972864 PriorityBoostEnabled : True PriorityClass : Normal PrivateMemorySize : 332496896 PrivateMemorySize64 : 332496896 PrivilegedProcessorTime : 00:00:30.6250000 ProcessName : powershell_ise ProcessorAffinity : 15 Responding : True SafeHandle : Microsoft.Win32.SafeHandles.SafeProcessHandle SessionId : 1 StartInfo : System.Diagnostics.ProcessStartInfo StartTime : 04.04.2018 08:55:57 Threads : {16712, 12844, 15764, 1992...} TotalProcessorTime : 00:02:26.2656250 UserProcessorTime : 00:01:55.6406250 VirtualMemorySize : 1491988480 VirtualMemorySize64 : 1491988480 WorkingSet : 363655168 WorkingSet64 : 363655168 Company : Microsoft Corporation CPU : 146,265625 Description : Windows PowerShell ISE FileVersion : 10.0.16299.15 (WinBuild.160101.0800) Path : C:\WINDOWS\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe Product : Microsoft® Windows® Operating System ProductVersion : 10.0.16299.15 PS>
Remove-EmptyProperty basically creates a new object with only the properties that have an actual value. Since it always creates a new object, as a side-effect, you will also see the properties that are hidden by default.