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


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


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.

$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.

1 Like

You can, but it has to be with splatting.

$a = @{
    Filter = '*'

Get-ADUser @a

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

I’d look at this:

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:

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

1 Like