Last active
April 8, 2021 16:51
-
-
Save bashenk/bc21a9879248f0a0b20ab9e55feac378 to your computer and use it in GitHub Desktop.
PowerShell function: Remove-Null - https://stackoverflow.com/questions/33038848/how-to-exclude-non-valued-object-properties-when-converting-to-json-in-powershel/63946256#63946256
This file contains hidden or 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
<# | |
.SYNOPSIS | |
Removes null property values from objects. | |
.DESCRIPTION | |
Removes property values that are null (or empty, unless -LeaveEmptyStrings parameter is use) from an object or array of objects. Additional items may be removed if they are passed as input to the -AlsoRemove parameter. | |
.EXAMPLE | |
[pscustomobject]@{Number=1;String="Test";Empty="";Null=$null} | Remove-Null | |
Number String | |
------ ------ | |
1 Test | |
Creates a custom object including empty/null values and then removes the null and empty values from the output object. | |
.EXAMPLE | |
[pscustomobject]@{Number=1;String="Test";Empty="";Null=$null} | Remove-Null -LeaveEmptyStrings -AlsoRemove "Test" | |
Number Empty | |
------ ----- | |
1 | |
Creates a custom object with empty/null values and then removes the null value and a specified value from the the output object. | |
.EXAMPLE | |
Get-CimInstance -ClassName Win32_UserProfile | Remove-Null -AlsoRemove 'Win32_FolderRedirectionHealth' | Select -First 2 | Format-Table | |
HealthStatus LastUseTime Loaded LocalPath RoamingConfigured SID | |
------------ ----------- ------ --------- ----------------- --- | |
3 9/18/2020 10:44:11 AM True C:\Users\DefaultAppPool False S-1-5-82-3006700770-424185619-1745488… | |
3 9/18/2020 10:44:11 AM True C:\Users\Administrator False S-1-5-21-540089215-2089150192-1846166… | |
Gets an array of CimInstance objects for Win32_UserProfiles and outputs an object with all property removed that are null, empty, consist of the string "Win32_FolderRedirectionHealth", or the ToString() method results with "Win32_FolderRedirectionHealth". | |
.INPUTS | |
System.Object or System.Object[] | |
You can pipe an object or array of objects to Remove-Null. | |
.OUTPUTS | |
System.Management.Automation.PSCustomObject or System.Object[] | |
Returns either a single object, or a collection of objects, corresponding to the input. | |
#> | |
Function Remove-Null { | |
[CmdletBinding()] | |
Param( | |
# Object from which to remove the null values. | |
[Parameter(ValueFromPipeline,Mandatory)] | |
$InputObject, | |
# Instead of also removing values that are empty strings, include them in the output. | |
[Switch]$LeaveEmptyStrings, | |
# Additional entries to remove, which are either present in the properties list as an object or as a string representation of the object. | |
# I.e. $item.ToString(). | |
[Object[]]$AlsoRemove=@() | |
) | |
Process { | |
# Iterate InputObject in case input was passed as an array | |
ForEach ($obj in $InputObject) { | |
$obj | Select-Object -Property ( | |
$obj.PSObject.Properties.Name | Where-Object { | |
-not ( | |
# If the property is null, remove it | |
$null -eq $obj.$_ -or | |
# If -LeaveEmptyStrings is not specified and the property is an empty string, remove it | |
(-not $LeaveEmptyStrings.IsPresent -and [string]::IsNullOrEmpty($obj.$_)) -or | |
# If AlsoRemove contains the property, remove it | |
$AlsoRemove.Contains($obj.$_) -or | |
# If AlsoRemove contains the string representation of the property, remove it | |
$AlsoRemove.Contains($obj.$_.ToString()) | |
) | |
} | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment