I'm trying to convert Colon Separated String to a PowerShell Dictionary. Following are the strings.
$inputkeyvalues = "Appsetting:true|environment:prod"
I have two key value pairs's in the $inputkeyvalues
variable and those are separated by pipe delimiter.
first one is: Appsetting:true
second one is: environment:prod
and I'm trying to convert to PowerShell dictionary. The final output should be something like,
Key Value
----- -----
Appsetting true
environment prod
$Dictionary= New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
Can someone please suggest me possible solution for this. Thanks in advance.
Use a hashtable
:
$inputkeyvalues = "Appsetting:true|environment:prod"
# Create hashtable
$Dictionary = @{}
# Split input string into pairs
$inputkeyvalues.Split('|') |ForEach-Object {
# Split each pair into key and value
$key,$value = $_.Split(':')
# Populate $Dictionary
$Dictionary[$key] = $value
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With