# parameterized function

**URL:** <https://forums.powershell.org/t/parameterized-function/531>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/parameterized-function/531 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/parameterized-function/531/1 "2011-12-31T18:30:00Z")

</div>

by powerpotion81 at 2013-01-27 18:27:50

> Ok, I am a newbie at powershell and I have been reading "Learn powershell in a month of lunches." I was doing good until I got to the scripting portion probably because I have no background in scripting. Anyway, I am having trouble with the parameterized function method, in particular in chapter 19, where I am supposed to be able to put in any computername and it will display those results for that particular machine. Here is the script I am using:  
>   
> function mycomputerinfo {  
> param (   
> $computername = ‘localhost’  
> )  
> $os=gwmi win32\_operatingsystem -ComputerName $computername  
> $bios=gwmi win32\_bios -comp $computername  
> $domain=gwmi win32\_computersystem -comp $computername  
> $obj= New-Object -TypeName psobject  
> $obj|Add-Member -MemberType noteproperty `<br> -name computername -value $computername<br>$obj|Add-Member -MemberType noteproperty `  
> -name VERSION -value ($os.caption)  
> $obj|Add-Member -MemberType noteproperty `<br> -name BIOS -value ($bios.serialnumber)<br>$obj|Add-Member -MemberType noteproperty `  
> -name DOMAIN -value ($domain.domain)  
> $obj|Add-Member -MemberType noteproperty `  
> -name HOSTNAME -value($domain.dnshostname)  
> Write-Output $obj  
> }  
> mycomputerinfo  
>   
>   
>   
> When I go to run the script i do my .\filename -computername _any computername_ and all I keep getting is _localhost_ for computername. I know what is happening, the $computername variable is keeping the localhost name, so why isn’t it changing like it said it would in the book? This is really frustrating me and help would be great thanks.

by nohandle at 2013-01-28 02:50:35

> `function mycomputerinfo {param ( $computername = 'localhost')	$computerName}mycomputerinfo -computername 'someComputerName'`  
> Works for me if I run it as function.   
>   
> If you save the script with the function in it, and then you call the script with parameter the parameter is not sent to the function call. Therefore the computername parameter returns the default value. you need to pass the script parameter values to the function parameter values.   
> if you call the script like this `.\filename.ps1 -computername 'csc0001'`  
> You need to call the function within the script like this: `mycomputerinfo -computername $Computername`

by powerpotion81 at 2013-01-28 05:20:27

> aaah, that makes sense, thanks for the response, works now.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:02pm UTC](https://forums.powershell.org/t/parameterized-function/531/2 "2024-05-16T21:02:18Z")

</div>

Closed on May 16, 2024, 9:02pm UTC


