PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.

As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Finding User Account with WMI

WMI represents all kinds of physical and logical entities on your machine. It also has classes that represent user accounts which include both local and domain accounts.

This piece of code returns the user account of the currently logged on user:

Get-WmiObject -Class Win32_UserAccount -Filter "Name='$env:username' and Domain='$env:userdomain'"

As always, the returned object holds a lot more information when you make PowerShell display all of its properties:

Get-WmiObject -Class Win32_UserAccount -Filter "Name='$env:username' and Domain='$env:userdomain'" 
| Select-Object * 

The returned information may look similar to this:

So you now can use all of this information to create test functions. The next function, for example, checks whether the currently logged on user is a local account or a domain account:

function Test-LocalUser
{   
    param(
      $UserName = $env:username,
      $Domain = $env:userdomain
    )

    (Get-WmiObject -Class Win32_UserAccount -Filter "Name='$UserName' and Domain='$Domain'")
    .LocalAccount
}

Twitter This Tip! ReTweet this Tip!

Copyright 2012 PowerShell.com. All rights reserved.