Search code examples
powershellwmiuser-accounts

Powershell -Filter not accepting two conditions


I have this command

$remoteuserlist = Get-WmiObject Win32_UserAccount `
-filter "LocalAccount =True" –computername $PC -verbose

that I am running to get a list of local accounts on a machine. I would also like to exclude the guest account from my list. so I tried something like this

$remoteuserlist = Get-WmiObject Win32_UserAccount `
-filter {LocalAccount =True -and Name -ne "Guest" –computername $PC -verbose}

but I get an invalid query error. Can someone explain my presumably blindingly obvious error?

Thanks


Solution

  • $remoteuserlist = Get-WmiObject Win32_UserAccount -filter {LocalAccount = "True" and Name != "Guest"} –computername $PC -verbose
    
    1. You were mixing WMI syntax and PowerShell syntax
    2. The brackets encompassing the filter were around the other parameters of Get-WmiObject