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
$remoteuserlist = Get-WmiObject Win32_UserAccount -filter {LocalAccount = "True" and Name != "Guest"} –computername $PC -verbose
- You were mixing WMI syntax and PowerShell syntax
- The brackets encompassing the filter were around the other parameters of
Get-WmiObject
The WQL "not equal" operator is != or <>.
WQL Operators
If you have a bunch of old VBScript WMI queries laying around you can use the Get-WMIObject -Query param to reuse them.
$remoteuserlist = Get-WmiObject -query "SELECT * FROM Win32_UserAccount WHERE LocalAccount = 'True' and Name != 'Guest'" –computername $PC -verbose
Not groundbreaking but it can help if you don't want to rewrite queries.