The PowerShell script discussed in this article will help you in querying for information about Windows user profiles on remote computers. The script returns user name, profile type, and in-use status of Windows profiles.

Sitaram Pamarthi

Sitaram Pamarthi is working as a Windows Engineer and his special fields of interest are PowerShell, Active Directory, Exchange, and virtualization.

I will show you how to query Windows user profile information for a remote computer using WMI and PowerShell. Since Windows Vista SP1, Microsoft has offered a WMI class named Win32_UserProfile to facilitate querying profile information for a remote computer. There are other ways to accomplish this task, such as listing the directories in the c:\users folder, but these methods are not efficient and might fail in cases where user profiles are stored in different drives or directories.

Querying using the Win32_UserProfile class will return a list of profile objects. These objects contain information such as the SID of the user to whom the profile belongs and the type of profile. Because the SID is not in a human-friendly format for identifying the user names, the script tries to convert the SID to a user name. This conversion will work for both domain and local user accounts when run locally on the computer. The SID-to-name conversion fails when you query profile information for local user accounts remotely. In such cases, you will see the SID number instead of a user name in the script output.

Another thing the script does is translate the profile type. The profile objects returned by the WMI query contain an attribute called Status. Per the Technet page, this attribute should contain values of 0, 1, 2, and 3. But what people have noticed is that the attribute stores values of 0, 1, 2, 4, and 8, where 1 = Temporary, 2 = Roaming, 4 = Mandatory, and 8 = Corrupted. The script output contains this profile type information as well.

Look at the PowerShell script below to understand how SID translation and profile type determination is done using PowerShell.

The script also gives you information such as whether the profile is in use or not and if the profile belongs to a system account such as SYSTEM. In addition, the script indicates whether the profile is in use or not.

Sample usage and output:

Query profile information on a local computer:

Query profile information for a remote computer:

Query profile information for multiple remote computers:

Get user profile information with PowerShell

Get user profile information with PowerShell

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

1+
Share
29 Comments
  1. Björn 6 years ago

    Hi,
    Found an error on line 17 in your code.

    should be:
    switch($profile.status)

    0

  2. Michael Pietroforte 6 years ago

    Björn, thanks! The line is now correct.

    0

  3. MIKEs84 5 years ago

    It's very nice script, thank you for this.

    As I am not familiar to scripting in PowerShell I will appreciate, if anyone could modify script to add some more feature which is usable for audit purposes.
    In my environment, we need to review accounts and also check if disabled or locked out.

    Could you, please, add 2 more columns into script to verify if account is disabled or locked out - on DC as well as on local computer ?

    Thanks for your effort. Appreciate it!

    David

    0

  4. Rebecca Stanke 5 years ago

    awesome script, can you add each users folder size?

    2+

  5. kevin 5 years ago

    AWESOME !!!! just what im looking for! thank you 1000x !!!

    0

  6. Rupa 3 years ago

    Hi,

    Can u send a script to delete those user profiles which is not used from more than 30 days excluding service, admin and network profiles.

    2+

  7. Josh 2 years ago

    I'd also like to have sizes included in the results.  Great job though.  Very useful for the environment I just came into.  Profile cleanups had never occurred before and this with the size should help me justify accomplishing it.

    2+

  8. Paul C 2 years ago

    Hi there

    Will it show temporary profiles by default if found?

    Thank you

    Paul

    1+

  9. Ravi D 2 years ago

    I am not able to run this file. I copied code in to notepad and saved as a Ps1 . but when i am trying to run it using powershell, i am getting multiple error where in it is working on powershell ISE. can you please help?

    0

    • Michael Pietroforte 2 years ago

      What error messages do you get?

      0

      • Ravi D 2 years ago

        I saved above script in notepad and given name as "WindowsProfiles.ps1"

        If i try to type Get-WindowsProfiles.ps1 then it gives me below error

        PS D:\> Get-WindowsProfiles.ps1
        Get-WindowsProfiles.ps1 : The term 'Get-WindowsProfiles.ps1' is not recognized as the name of a cmdlet, function,
        script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path
        correct and try again.
        At line:1 char:1
        + Get-WindowsProfiles.ps1
        + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : ObjectNotFound: (Get-WindowsProfiles.ps1:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

        if i run script without "Get-" then it gives me result

        but my main question is how i can run same script for multiple servers

        0

      • D 2 years ago

        I saved above script in notepad and given name as "WindowsProfiles.ps1"

        If i try to type Get-WindowsProfiles.ps1 then it gives me below error

        PS D:\> Get-WindowsProfiles.ps1
        Get-WindowsProfiles.ps1 : The term 'Get-WindowsProfiles.ps1' is not recognized as the name of a cmdlet, function,
        script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path
        correct and try again.
        At line:1 char:1
        + Get-WindowsProfiles.ps1
        + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : ObjectNotFound: (Get-WindowsProfiles.ps1:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

        if i run script without "Get-" then it gives me result

        but my main question is how i can run same script for multiple servers

        0

        • Clayton 2 years ago

          Since you are saving the file as "WindowsProfiles.ps1" that will be what you need to call in the console. in the example above, the author saved the file as "Get-WindowsProfiles.ps1". That is why you are getting the error you stated.

          3+

          Users who have LIKED this comment:

          • avatar
          • avatar
          • Ravi d 2 years ago

            Thank you so much for guidance ....I thought he used get command

            0

  10. Joseph 2 years ago

    How do we add this line to get the remote computers? I'm a bit of a Powershell noob and could use some assistance. 🙂

    \Get-WindowsProfiles.ps1 -ComputerName (get-content c:\temp\servers.txt)

    0

  11. Joseph 2 years ago

    I figured it out. I made a ps1 file for the program itself, a text servers list and then used the code for executing it and everything works perfect.

    I have another question though. Is there a way to get all this information output in to an Excel spreadsheet or at least a notepad for easy searching and access?

    Thank you very much! 🙂

    0

  12. Aidan 2 years ago

    Hi,

    Apologies just a novice at this stage using powershell. Love your scrip exactly what I needed for my program I am working on.

    With your script is there away to output the output to an array that I can add not a list box (Win Gui)

    Thanks heaps

     

    0

    • Michael Pietroforte 2 years ago

      Aidan, there is no need to apologize for being a newbie. 🙂 The script's output is not a GUI, the output is an object and you can use the object's properties in your script. Objects are much easier to manage than arrays. If you want to work with PowerShell, I highly recommend to learn how to use classes and objects.

      1+

      Users who have LIKED this comment:

      • avatar
  13. Robert 1 year ago

    Script works great. I had one question though.  Is there a way i can get the output of the command to go into a CSV with each output object is its own colum?

    Thanks

    0

  14. Robert 1 year ago

    Nevermind my question above, i figured it out.

    I was able to export and have it format as i wanted by assigning the output a variable and then outputting the variable to csv:

    $results = .\Get-WindowsProfiles.ps1 -ComputerName (get-content c:\temp\test.txt)
    $results | export-csv -Path C:\temp\test.csv

    2+

  15. Nandeesha 1 year ago

    its not working for 2003 servers. what need to modify ?

    0

  16. David Figueroa 1 year ago

    It won't work with 2003.  The WMI class didn't exist in Win2k3.  :-\

    David F.

    1+

  17. John B. 1 year ago

    When I try to run the script for a remote computer by adding the ComputerName variable, nothing happens. It works fine on the local machine.

    There's no error, no output in powershell at all. I'm attempting to query other computers on our domain.

    0

    • Maybe the Windows Firewall blocks the connection. Check if you can access the remote machine with the Computer Management tool.

      0

      • John B. 1 year ago

        Thank you. I tried using the Computer Management tool as you suggested and cannot connect that way either. I think you're right in that it is being blocked by our company antivirus.

        That solves the mystery, thanks again!

        0

  18. warren klaus 7 months ago

    Great script!

    when i run it against a list of computers, it does nothing.   if i run against individual computers, it works.   

    Also, how can i exclude the system profiles.

     

    thanx again.  

    0

  19. David Figueroa 7 months ago

    This is a tangent question, but this script is the perfect source for it.. 

    Given this section:

    Would this perform better with  hashtables?

    David F. 

    1+

  20. David Figueroa 7 months ago

    Warren Klaus:

    I can't help you with why you aren't getting remote results, I can help you with the Special Profiles..

    Change this line:

    to this: 

    David F.

    1+

    Users who have LIKED this comment:

    • avatar
  21. Jon 2 weeks ago

    Is it possible to list the date when each profile was created?

    I need a script that will identify profiles that were created after a certain date so that I can then remove them.

    (For OS rollback)

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account