I needed a simple and reusable PowerShell code to convert any object into hash table that can be copy and pasted as source code. I wrote this:

$outputItem = '@{'

$MyCustomObject.PSObject.Properties.Name |
    Where-Object -FilterScript { $_ -ne 'ComputerName' } | ForEach-Object -Process `
    {
        if ($interfaceItemIpConfigurationItem.$_ -is [bool])
        {
            $valueItem = '${0}' -f $MyCustomObject.$_.ToString().ToLower()
        }
        else
        {
            $valueItem = "'{0}'" -f ($MyCustomObject.$_ -join "', '")
        }

        $outputItem += "`r`n{0} = {1}" -f $_.Padright(16, ' '), $valueItem
    }

$outputItem += "`r`n}"

# Output
$outputItem
« Previous Next »