Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Last active February 10, 2019 16:12
Show Gist options
  • Save nohwnd/c97368c7ee2cce5ba461b6f564ed9149 to your computer and use it in GitHub Desktop.
Save nohwnd/c97368c7ee2cce5ba461b6f564ed9149 to your computer and use it in GitHub Desktop.
Add or update to hashtable
function getOrUpdateValue {
[CmdletBinding()]
param(
$Hashtable,
$Key,
$DefaultValue
)
if ($Hashtable.ContainsKey($Key)) {
# do not enumerate so we get the same thing back
# even if it is a collection
$PSCmdlet.WriteObject($Hashtable.$Key, $false)
}
else {
$Hashtable.Add($Key, $DefaultValue)
# do not enumerate so we get the same thing back
# even if it is a collection
$PSCmdlet.WriteObject($DefaultValue, $false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment