A parameter cannot be found that matches parameter name 'ComputerName'

Isaac Sizemore 41 Reputation points
2022-01-17T23:32:10.31+00:00

I'm working through a powershell book and am trying to write a simple function which is:
Function Install-Software {
[cmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateSet('1','2')]
[string]$Version,

[Parameter(Mandatory)]
[string]$ComputerName
)

Write-Host "I installed software version $Version on $ComputerName. Yay!"
}

It is written in powershell ISE (i know not supported, but it worked fine before i added the computername parameter).
In powershell i first typed the location of the script as i did before: C:\Install-Software.ps1
then called the function with the parameters:
Install-Software -Version 2 -ComputerName SRV1
I received an error that states "Install-Software : A parameter cannot be found that matches parameter name 'ComputerName'.
At line: 1 char: 29

I'm not sure whats wrong, the code works fine even if I don't include this parameter when calling the function. For example, if I type:
Install-Software -Version 2
Then the console outputs as it should "I installed software version 2. Yay!"
Not sure why it says the parameter computername can't be found, I have copied the exact code from the book I'm going through. Please help, Thanks!

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,580 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 34,761 Reputation points
    2022-01-18T13:55:04.303+00:00

    Remove the Function {} definition so that the parameters apply to the script.

    [cmdletBinding()]  
    param(  
    [Parameter(Mandatory)]  
    [ValidateSet('1','2')]  
    [string]$Version,  
      
    [Parameter(Mandatory)]  
    [string]$ComputerName  
    )  
      
    Write-Host "I installed software version $Version on $ComputerName. Yay!"  
    

    I called your script test4.ps1 to play with.

    166049-capture.jpg


2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,466 Reputation points
    2022-01-18T02:48:06.72+00:00

    If the code you posted is in the script editor pane of the ISE, put the cursor into that pane and hit the "F5" key. Then put the cursor into the PowerShell console pane and test your function with "Install-Software -Version 2 -ComputerName SRV1".

    Just having the code in the script editor pane doesn't put the code into memory. My guess is that the last time you ran the function (not called the function), it was the earlier version of the function (the one without the ComputerName parameter).


  2. Shane Walsh 31 Reputation points
    2022-01-18T08:46:49.117+00:00

    This worked for me in ISE @Isaac Sizemore

    Function Install-Software {

    [cmdletBinding()]

    param(

    [Parameter(Mandatory=$true)]
    [ValidateSet('1','2')]
    [string]$Version,

    [Parameter(Mandatory=$true)]
    [string]$ComputerName
    )

    Write-Host "I installed software version $Version on $ComputerName. Yay!"

    }

    165982-success.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.