Skip to content

Instantly share code, notes, and snippets.

@scriptingstudio
Last active June 3, 2025 02:31
Show Gist options
  • Save scriptingstudio/3d637d12016da9836451ee9721757d17 to your computer and use it in GitHub Desktop.
Save scriptingstudio/3d637d12016da9836451ee9721757d17 to your computer and use it in GitHub Desktop.
Get ValueFromRemainingArguments/Args as HashTable
function test-remaining {
[cmdletbinding()]
param (
[parameter(ValueFromRemainingArguments)]
$remaining
)
$htRemaining = [ordered]@{}
$unnamed = [System.Collections.Generic.list[object]]::new()
$remaining | ForEach-Object {
if ($_ -match '^-(?<param>[^:]+):?$') {
# New parameter
$lastpar = $matches['param']
$htRemaining[$lastpar] = $true # Default value for switch
} elseif ($lastpar) {
# Update value to next argument
$htRemaining[$lastpar] = $_
$lastpar = $null
} else {
$unnamed.add($_)
}
}
if ($unnamed.count) {$htRemaining['unnamed'] = $unnamed}
$htRemaining
}
function convert-args {
$htRemaining = [ordered]@{}
$unnamed = [System.Collections.Generic.list[object]]::new()
$args | ForEach-Object {
if ($_ -match '^-(?<param>[^:]+):?$') {
# New parameter
$lastpar = $matches['param']
$htRemaining[$lastpar] = $true # Default value for switch
} elseif ($lastpar) {
# Update value to next argument
$htRemaining[$lastpar] = $_
$lastpar = $null
} else {
$unnamed.add($_)
}
}
if ($unnamed.count) {$htRemaining['unnamed'] = $unnamed}
$htRemaining
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment