# Using Win32_UserAccount WMI filter in PowerShell/Group Policies and what to avoid

Some months ago, I created PowerShell Script to create local administrative users on workstations – Create a local user or administrator account in Windows using PowerShell. It’s a bit overcomplicated, but the goal was it should work for Windows 7 and up, and that means supporting PowerShell 2.0. As part of that exercise, I’ve been using Win32_UserAccount WMI based query to find local users and manage them to an extent. While Get-LocalUser exists, it’s not suitable for the PowerShell 2.0 scenario. I also use the same query in GPO for WMI filtering. You can say it’s been a good friend of mine.

blog

02 Jun 2020 en

![Using Win32_UserAccount WMI filter in PowerShell/Group Policies and what to avoid](/wp-content/uploads/2020/06/img_5ed5f518efd15.png)

[View images (3)](#pf-content-image-1-0)

Some months ago, I created **PowerShell Script** to **create local administrative users** on workstations – [Create a local user or administrator account in Windows using PowerShell](https://evotec.xyz/create-a-local-user-or-administrator-account-in-windows-using-powershell/). It's a bit overcomplicated, but the goal was it should work for Windows 7 and up, and that means supporting PowerShell 2.0. As part of that exercise, I've been using **Win32_UserAccount** WMI based query to find local users and manage them to an extent. While **Get-LocalUser** exists, it's not suitable for the **PowerShell 2.0** scenario. I also use the same query in **GPO for WMI filtering**. You can say it's been a good friend of mine – until today! Let's take a look at this basic WMI query:

```powershell
Get-WmiObject -Query "SELECT * FROM Win32_UserAccount WHERE LocalAccount=true" | Format-Table
```

[!\[Win32_UserAccount WMI query output listing local accounts returned by Get-WmiObject in PowerShell.\](/wp-content/uploads/2020/06/img_5ed5f518efd15.png)](/wp-content/uploads/2020/06/img_5ed5f518efd15.png)

It can also give more relevant data, such as if the account is **enabled **or **disabled**.

```powershell
Get-WmiObject -Query "SELECT * FROM Win32_UserAccount Where LocalAccount = true" | Format-Table Caption, Domain, Name, PasswordChangeable, PasswordRequired, Disabled
```

I've been using this **WMI **query both in PowerShell scripts that need to support **PowerShell 2.0 (Windows 7)** and in **GPO WMI** filtering when I apply GPO that only should execute if a given user exists.

[!\[Win32_UserAccount query with selected properties showing whether local accounts are enabled or disabled.\](/wp-content/uploads/2020/06/img_5ed5f4fd3e1d2.png)](/wp-content/uploads/2020/06/img_5ed5f4fd3e1d2.png)

### 💡 WMI Win32_UserAccount – Why even mention it?

Now you may be wondering why I even mention this? I was checking for the existence of a single local user on a workstation rather than asking for multiple users. So by merely adding **Name = ‘Administrator'** I'm making sure my query outputs only a single user.

```powershell
Get-WmiObject -Query "SELECT * FROM Win32_UserAccount Where LocalAccount = true AND Name = 'Administrator'" | ft
```

Except now, instead of **1 second**, it takes **2 minutes**.

### 💡 WMI Win32_UserAccount – Why that happens?

To understand why would there be a difference between those two queries

```powershell
Get-WmiObject -Query 'Select * FROM Win32_UserAccount WHERE LocalAccount = true' | ft
Get-WmiObject -Query "SELECT * FROM Win32_UserAccount Where LocalAccount = true AND Name = 'Administrator'" | ft
```

I've run those, but without any **WHERE** filtering.

```powershell
Get-WmiObject -Query 'Select * FROM Win32_UserAccount' | ft
```

And suddenly, everything made sense. Without **WHERE** filtering, it queries not only local users but **Active Directory** users as well. This explains why a simple query would t**ake 2 minutes – I have over 50000 users** in my **AD**. Well, it only sort of makes sense because we're using an almost same query, which has a very subtle difference that shouldn't impact query in that way. If anything, it should be faster because we added condition limiting our output.

[!\[Unfiltered Win32_UserAccount query returning Active Directory users as well as local accounts, explaining the slowdown.\](/wp-content/uploads/2020/06/img_5ed55d4591bab.png)](/wp-content/uploads/2020/06/img_5ed55d4591bab.png)

So, while I could probably find some way to work around this issue in **PowerShell**, it doesn't solve my problem when using the very same query in **WMI filtering for Group Policies**. It means that each time the GPO gets executed, it takes **2 minutes+** to do an assessment, whether it's valid for the current workstation or not. Not to mention, it impacts the performance of an AD.

### 💡 WMI Win32_UserAccount – Workaround

What's the fix? Changing **equal (**=**)** to **LIKE**. Consider those two queries:

```powershell
Get-WmiObject -Query "SELECT * FROM Win32_UserAccount Where LocalAccount = true AND Name Like 'Administrator'" | ft
Get-WmiObject -Query "SELECT * FROM Win32_UserAccount Where LocalAccount = true AND Name = 'Administrator'" | ft
```

Almost the same, but the first one takes **1 second**, the second one **2 minutes**. So for some weird reason, the equal sign is causing **WMI provider **to go nuts, and changing it to **LIKE **resolves the issue. While I don't know why that happens, I'm pretty happy with this solution. I'm pretty sure I will forget about it in a few days so that this blog post will be my reminder to not take things for granted and that even subtle difference requires extensive testing.

## Related Projects

Projects that align with this post's tags, categories, and content.

[All projects](/projects/)

Matching projects...

[Back to Blog](/blog)

## Related posts

- [Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster](/blog/mastering-active-directory-hygiene-automating-stale-computer-cleanup-with-cleanupmonster/)
- [Report Active Directory Accounts that are Synchronized with Azure AD](/blog/report-active-directory-accounts-that-are-synchronized-with-azure-ad/)
- [Strengthening Password Security in Active Directory: A PowerShell-Powered Approach](/blog/strengthening-password-security-in-active-directory-a-powershell-powered-approach/)

<!-- PowerForge markdown artifact sha256:36ED3B673A53F05C3A591C21F1B30C03CF790FF264C02E29C8B96C40FE0EADB3 -->
