# 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 pl

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

[Zobacz obrazy (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
```

[!\[Wynik zapytania WMI Win32_UserAccount pokazujacy lokalne konta zwrocone przez Get-WmiObject w 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.

[!\[Zapytanie Win32_UserAccount z wybranymi wlasciwosciami pokazujace, czy lokalne konta sa wlaczone lub wylaczone.\](/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.

[!\[Niefiltrowane zapytanie Win32_UserAccount zwracajace uzytkownikow Active Directory oraz konta lokalne, co wyjasnia spowolnienie.\](/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.

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:96965571BF72FFEE0960C29624D3C00BF9DDC0DD5D57CB152541D107A64151D3 -->
