# How to pass the string from variable instead using it as parameter

**URL:** <https://forums.powershell.org/t/how-to-pass-the-string-from-variable-instead-using-it-as-parameter/21990>\
**Category:** PowerShell Help\
**Created:** [March 28, 2023, 6:18pm UTC](https://forums.powershell.org/t/how-to-pass-the-string-from-variable-instead-using-it-as-parameter/21990 "2023-03-28T18:18:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tnx-cz](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tnx-cz](https://forums.powershell.org/u/tnx-cz)\
**Post date:** [March 28, 2023, 6:18pm UTC](https://forums.powershell.org/t/how-to-pass-the-string-from-variable-instead-using-it-as-parameter/21990/1 "2023-03-28T18:18:48Z")

</div>

Hello.

How do I make line 2 work so that it extracts the string from $a and uses it in the command instead of trying to find a user with identity $a?

$a = “-filter \*”  
get-aduser $a

And how is this called? I could not search for help because I do not know how it is called.  
Thank you

Honza

---

<div class="post-metadata">

**Author:** ![mrfloppy](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mrfloppy](https://forums.powershell.org/u/mrfloppy)\
**Post date:** [March 28, 2023, 7:07pm UTC](https://forums.powershell.org/t/how-to-pass-the-string-from-variable-instead-using-it-as-parameter/21990/2 "2023-03-28T19:07:58Z")

</div>

Don’t think you can put the parameter in a variable and call it.  
You can put the input of the parameter in a variable.

I’m assuming this isn’t what you’re trying to do and there’s more to the script, because that variable and command isn’t necessary to put in a variable.

If you’re actually trying to do something more complex, you can put the info in a variable and call it like so.

```auto
$a = '*'
get-aduser -filter $a

```

So if you have a criteria for the filter, you can put that in a variable. But still have to define -filter parameter in the command.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [March 28, 2023, 8:40pm UTC](https://forums.powershell.org/t/how-to-pass-the-string-from-variable-instead-using-it-as-parameter/21990/3 "2023-03-28T20:40:46Z")

</div>

You can, but it has to be with splatting.

```auto
$a = @{
    Filter = '*'
}

Get-ADUser @a

```

---

<div class="post-metadata">

**Author:** ![tnx-cz](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tnx-cz](https://forums.powershell.org/u/tnx-cz)\
**Post date:** [April 3, 2023, 9:36am UTC](https://forums.powershell.org/t/how-to-pass-the-string-from-variable-instead-using-it-as-parameter/21990/4 "2023-04-03T09:36:16Z")

</div>

Found it.  
$a = “-filter \*”  
Invoke-Expression “get-aduser $a”

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [April 3, 2023, 2:59pm UTC](https://forums.powershell.org/t/how-to-pass-the-string-from-variable-instead-using-it-as-parameter/21990/5 "2023-04-03T14:59:32Z")

</div>

I’d look at this:

> **[Invoke-Expression (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-expression?view=powershell-7.3#notes)**
>
> The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression, a string submitted at the command line is returned (echoed) unchanged. Expressions are...

particularly where it says this: In most cases, you invoke expressions using PowerShell’s call operator and achieve the same results. The call operator is a safer method.

and this:

> **[about Operators - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.3#call-operator-)**
>
> Describes the operators that are supported by PowerShell.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 3, 2023, 4:27pm UTC](https://forums.powershell.org/t/how-to-pass-the-string-from-variable-instead-using-it-as-parameter/21990/6 "2023-04-03T16:27:53Z")

</div>

> [@tnx-cz](#):
>
> $a = “-filter \*”  
> Invoke-Expression “get-aduser $a”

The actual quesion for me would be “_ **Why** _”? What problem do you want to resolve with this approach?

---

<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:14pm UTC](https://forums.powershell.org/t/how-to-pass-the-string-from-variable-instead-using-it-as-parameter/21990/7 "2024-05-16T20:14:59Z")

</div>

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


