How to pass dynamic parameters in Powershell? How to pass dynamic parameters in Powershell? powershell powershell

How to pass dynamic parameters in Powershell?


You can just pass your variables as arguments to Write-Host:

Write-Host -Fore $forecolor -NoNewLine:(!$newline) 'Hello World'

For a truly dynamic way you can use a hashtable:

$params = @{ NoNewLine = $true; ForegroundColor = 'Green' }

and then use the splat operator

Write-Host @params Hello World

You can add parameters and their values to the hashtable as you like before calling Write-Host that way.