# Inserting string into command

**URL:** <https://forums.powershell.org/t/inserting-string-into-command/9115>\
**Category:** PowerShell Help\
**Created:** [August 2, 2017, 10:10am UTC](https://forums.powershell.org/t/inserting-string-into-command/9115 "2017-08-02T10:10:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ertuu85](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@ertuu85](https://forums.powershell.org/u/ertuu85)\
**Post date:** [August 2, 2017, 10:10am UTC](https://forums.powershell.org/t/inserting-string-into-command/9115/1 "2017-08-02T10:10:03Z")

</div>

Hello, why doesnt something like this work?

```
$query = "$_.passwordneverexpires -eq $true"

(Get-ADUser -filter * -Properties PasswordNeverExpires,passwordlastset,lastlogontimestamp | ?{$_.enabled -eq $true} | select name,PasswordNeverExpires,passwordlastset,@{name = 'LastLogon';Expression = ({get-date $_.lastlogontimestamp -UFormat '%D %R'})} | Where-Object { **$query** )})
```

I see that when put in "'s it shows the command, and runs if i were to copy paste, but it just display all results, not the pipeline item in $query?

And how can i get it to execute?

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [August 2, 2017, 10:15am UTC](https://forums.powershell.org/t/inserting-string-into-command/9115/2 "2017-08-02T10:15:12Z")

</div>

Strings aren’t automatically processed as code - they’re strings. Even in double quotes with a subexpression, the result is a string, not the $True/$False that Where-Object needs to see.

$query = {$\_.passwordneverexpires -eq $true}

Defines a script block, which is code. You could probably pass that directly to the Where-Object parameter…

Where-Object $query

You could possibly also use the call operator ([Call operator - Run - PowerShell - SS64.com](https://ss64.com/ps/call.html)) to “invoke” the string, although that’s kind of a poor practice in terms of readability.

I’ll also note that in this specific example, you’re probably not in an efficient model. You’ve already got a Where-Object clause; just do all your filtering there. And, it might be worth writing an -LdapFilter instead, for Get-ADUser, so you could do all your filtering right on the DC.

---

<div class="post-metadata">

**Author:** ![ertuu85](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@ertuu85](https://forums.powershell.org/u/ertuu85)\
**Post date:** [August 2, 2017, 1:22pm UTC](https://forums.powershell.org/t/inserting-string-into-command/9115/3 "2017-08-02T13:22:38Z")

</div>

Thanks Don, I really appreciate the advice!

I was able to get it after using an ‘invoke-expression’ cmdlet and minor altering, but you make good points on using the ldapfilter, i’ll look into it.

Thanks again!

---

<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:37pm UTC](https://forums.powershell.org/t/inserting-string-into-command/9115/4 "2024-05-16T20:37:50Z")

</div>


