Skip to content

Instantly share code, notes, and snippets.

@Splaxi
Created January 31, 2020 09:21
Show Gist options
  • Save Splaxi/ec4667fd83bf4cd995e0e248f42612d2 to your computer and use it in GitHub Desktop.
Save Splaxi/ec4667fd83bf4cd995e0e248f42612d2 to your computer and use it in GitHub Desktop.
Convert Hashtable to string to share hashtable with others
function Convert-HashTableToString {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidDefaultValueSwitchParameter", "")]
[CmdletBinding()]
[OutputType([System.String])]
param (
[HashTable] $InputObject,
[string] $KeyPrefix = "",
[string] $ValuePrefix = " = ",
[switch] $KeepCase = $true
)
$output = New-Object System.Collections.Generic.List[string]
$output.Add("`$parms = @{")
foreach ($key in $InputObject.Keys) {
$value = "{0}" -f $InputObject.Item($key).ToString()
if (-not $KeepCase) {$value = $value.ToLower()}
$output.Add("$KeyPrefix$($key)$ValuePrefix`"$($value)`"")
}
$output.Add("}")
$output.ToArray()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment