Maraudingas
Maraudingas

Reputation: 118

Get-ADComputer -Filter name not taking variable for computer search

I've tried to create a powershell script which allows me to find the correct computer by it's service tag in AD. So far I could do it by writing the service tag in the script, but when I wanted to pass it with variable, it doesn't let me do it, and shows this error:

Get-ADComputer : A positional parameter cannot be found that accepts argument 'GCX0YY1"'. At line:2 char:1 + Get-ADComputer -Filter 'Name -like "*'$name'"'

$name = Read-Host 'Write the computers service tag'
Get-ADComputer -Filter 'Name -like "*'$name'"'

Upvotes: 4

Views: 5649

Answers (2)

Richard
Richard

Reputation: 7000

Get-ADComputer -Filter "Name -like '*$name'"

Upvotes: 2

Martin Brandl
Martin Brandl

Reputation: 58961

The $name variable doesn't get resolved if you use single quotes on your string. You could use a string format (alias -f) to get your variable in place:

$name = Read-Host 'Write the computers service tag'
Get-ADComputer -Filter ('Name -like "{0}"' -f $name)

Upvotes: 3

Related Questions