Transform a PowerShell Object with empty properties to an Hashtable with only properties that have value

Hi guys,

Some time you may want to use PowerShell splating instead of PipeLine input.
Splating, is the action to add some parameters to a cmdlet directly by giving the cmdlet a hashtable with properties and values.

Here, as an example, the creation of an MsolUser using splating for my cat:
SplatingExample01Sometime, when you use splating to create/edit objects you use come Get-Noun cmdlet that provides you with an object that may or may not have empty properties. If you use the object, with empty properties, the cmdlet to create/edit what you want will manly fail.

To avoid the failure you may want to remove the empty properties from the Object and turn the Object in an HashTable. Here is the way that I made it for my current project:

[ps]$return = @{}
foreach ($attribute in $MyObject.psobject.properties) {
if ($attribute.value) {
$return.Add($attribute.name,$attribute.value)
}
}
Write-Output $return[/ps]

The return variable will then be an HashTable with all none empty properties. This object will be perfect to set or create any kind of object using splating.