# Pass variable to a parameter

**URL:** <https://forums.powershell.org/t/pass-variable-to-a-parameter/12266>\
**Category:** PowerShell Help\
**Created:** [March 26, 2019, 2:06pm UTC](https://forums.powershell.org/t/pass-variable-to-a-parameter/12266 "2019-03-26T14:06:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chexmix](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@chexmix](https://forums.powershell.org/u/chexmix)\
**Post date:** [March 26, 2019, 2:06pm UTC](https://forums.powershell.org/t/pass-variable-to-a-parameter/12266/1 "2019-03-26T14:06:23Z")

</div>

I want to pass a single computer to the machines parameter or all computers from an OU. If I create a variable before the parameter the param doesn’t work.

The param will not work with the $All variable if I remove that line it works with a single computer.

```
$All = Get-ADComputer -filter * -SearchBase "OU=Servers,DC=ad,DC=test,DC=ad" | select -ExpandProperty name

param(
$machines
)
$report = @()
$object = @()
foreach($machine in $machines)
{
$object = gwmi win32_operatingsystem -ComputerName $machine | select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
$report += $object
}
$report | sort lastbootuptime
```

---

<div class="post-metadata">

**Author:** ![pwshliquori](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pwshliquori/32/222_2.png) [@pwshliquori](https://forums.powershell.org/u/pwshliquori)\
**Post date:** [March 26, 2019, 2:15pm UTC](https://forums.powershell.org/t/pass-variable-to-a-parameter/12266/2 "2019-03-26T14:15:30Z")

</div>

The `param ()` needs to go to the top of your script. Define the variable in the console then call the script with the `-Machines` parameter.

Also, try to be consistent with Microsoft’s parameter names. Instead of using `-Machine` use `-ComputerName`

pwshliquori

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [March 26, 2019, 2:51pm UTC](https://forums.powershell.org/t/pass-variable-to-a-parameter/12266/3 "2019-03-26T14:51:47Z")

</div>

If you’re trying to give the parameter a default value for when you call the script without specifying, you need to assign to the parameter _inside_ the parameter block instead. Below is one way you can write it.

```
[CmdletBinding()]
param(
    [Parameter()]
    [string[]]
    $ComputerName = (Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=ad,DC=test,DC=ad" | Select-Object -ExpandProperty Name)
)

$Report = foreach($Machine in $ComputerName) {
    Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $Machine |
        Select-Object -Property CSName, @{ Name = 'LastBootUpTime'; Expression = {$_.ConverttoDateTime($_.LastBootUpTime)} }
}

$Report | Sort-Object -Property LastBootUpTime
```

---

<div class="post-metadata">

**Author:** ![chexmix](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@chexmix](https://forums.powershell.org/u/chexmix)\
**Post date:** [March 26, 2019, 3:09pm UTC](https://forums.powershell.org/t/pass-variable-to-a-parameter/12266/4 "2019-03-26T15:09:17Z")

</div>

Joel, that is perfect. Thanks

---

<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, 8:33pm UTC](https://forums.powershell.org/t/pass-variable-to-a-parameter/12266/5 "2024-05-16T20:33:05Z")

</div>

Closed on May 16, 2024, 8:33pm UTC


