Last active
July 8, 2026 12:47
-
-
Save scriptingstudio/d497e3a7a8d6324d368b27da8bf7063c to your computer and use it in GitHub Desktop.
Convert $PSBoundParameters to command line string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Convert-BoundParameters ([hashtable]$parameters) { | |
| if (-not $parameters.count) {return} | |
| $params = [System.Text.StringBuilder]::new() | |
| $null = $parameters.GetEnumerator().foreach{ | |
| $key,$val = $_.Key,$_.Value | |
| if ($val -is [System.Management.Automation.SwitchParameter]) { | |
| if ($val.IsPresent) {$params.Append(" -$key")} | |
| } | |
| elseif ($val -is [string]) { | |
| if ($val.contains(' ') -or [string]::IsNullOrEmpty($val)) {$val = "'$val'"} | |
| $params.Append(" -$key $val") | |
| } | |
| elseif ($val -is [hashtable]) { | |
| $ht = $val.GetEnumerator().foreach{"'{0}'='{1}';" -f $_.Key,$_.Value} | |
| $params.Append(" -$key @{$ht}") | |
| } | |
| elseif ($val -is [array]) { | |
| $ar = $val.foreach{ | |
| elseif ($_ -is [string] -and | |
| ($_.contains(' ') -or [string]::IsNullOrEmpty($_))) {"'$_'"} else {$_} | |
| } -join ',' | |
| $params.Append(" -$key $ar") | |
| } | |
| elseif ($val -is [scriptblock]) {$params.Append(" -$key {$val}")} | |
| else { | |
| $params.Append(" -$key $val") | |
| } | |
| } | |
| $params.ToString() | |
| } # END Convert-BoundParameters |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment