0

I'm not a PowerShell expert. I wrote a short script to obtain from AD all the computers that contain in their name a substring that is entered by the user. I have not been able to concatenate the pipelined commands with the $substring variable, which causes PowerSell to achieve nothing. How could I correctly concatenate the variable to the command?

$substring = Read-Host 'Enter search substring. All computers whose name contains that substring will be recovered: '
msg console /server:localhost "An Excel file named 'Results' will be creatd at C:\Temp\Results.xlsx"
$command =  Get-ADComputer -Filter {Name -like "*$substring*"} | Select -Property Name | Export-Excel -Path C:\Temp\LOAN.xlsx
Read-Host "Done! Enter any key to exit"
2
  • -Filter {Name -like "*$substring*"} will fail, you need to use a string. Commented yesterday
  • While seductively convenient, the use of script blocks ({ ... }) as -Filter arguments is conceptually problematic and can lead to misconceptions. See the linked duplicates for details.
    – mklement0
    Commented yesterday

0

Browse other questions tagged or ask your own question.