You don't need quotes around the variable, so just change this:
Get-ADComputer -Filter {name -like '$nameregex' -and Enabled -eq "true"}
in it:
Get-ADComputer -Filter {name -like $nameregex -and Enabled -eq "true"}
Please note, however, that the script notation for filter statements is misleading because the statement is actually a string, so itβs better to write it as such:
Get-ADComputer -Filter "name -like '$nameregex' -and Enabled -eq 'true'"
Related to this . Also related .
And FTR: here you are using a wildcard (operator -like ), not regular expressions (operator -match ).
source share