Why isn't my PowerShell function accepting parameters?

April 24, 2025 1 answer
Asked by census worksheet
I'm having trouble with a PowerShell function I wrote. I've got other functions working fine, but this one isn't accepting the parameter as expected. Here's the code: ```powershell # 1. Set input variable $domainInput = "test" # 2. Define the function function Set-Domain { param ( [string]$input ) Write-Host "Input is: $input" if (-not $input) { Write-Host "[ERROR] No input was found." } if ($input -eq "true") { return "@dynamicdomain.dk" } else { return "@$input" } } # 3. Call the function Write-Host $domainInput Set-Domain -input $domainInput Write-Host "Result: $domain" Set-Domain -input "true" ``` The output I'm getting is confusing: ``` test Input is: [ERROR] No input was found. @ Result: @ Input is: [ERROR] No input was found. @ ``` I can't figure out why `$input` has no value inside the function. Can anyone help?

Answers

out-of-bounds-
The issue here is that `$input` is a reserved automatic variable in PowerShell. That means it's already being used by the system, which is why it doesn't hold your assigned value. Try renaming your parameter to something else, like `$input1`, and it should work just fine!

+please proceed: Yes, and if you were using VS Code, it would have warned you about using a reserved name.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions