I recently came across a requirement to list the User profiles in remote computer. I can do a quick dir of \\pc1\c$\users to get the list of profiles but that is not efficient. It is quite possible that profiles are placed in different drive/directory. After some research, I stumbled on a WMI class WIn32_UserProfile using which I can get the profiles list I wanted. I read about this class and what it can offer. Interestingly, I can get more details about a profile with this class. I can check if a profile is local, roaming, temporary, random or a corrupted one. This data is useful various scenarios. Also I can check if a profile is a human profile or it belongs to system (that means system is maintaining it — ex: networkservice).
So, the script discussed in this article will help you to get list of user profiles on remote computer and what type of profile it is, profile path (where the profile is stored), and will tell you if the profile is non-system profile or system profile.
One thing I must note here is, Win32_UserProfile WMI class is available in systems greater than Windows Vista/Windows 2008. The previous Operating Systems are not having this class. It means that script will not work for any computers having prior operating systems like Windows 2003/2000/XP, etc.
Script : Get-UserProfile.ps1
[cmdletbinding()] param ( [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] [string[]]$ComputerName = $env:computername, [string]$UserName ) foreach ($Computer in $ComputerName) { $Profiles = Get-WmiObject -Class Win32_UserProfile -Computer $Computer -ea 0 foreach ($profile in $profiles) { $objSID = New-Object System.Security.Principal.SecurityIdentifier($profile.sid) $objuser = $objsid.Translate([System.Security.Principal.NTAccount]) switch($status.Status) { 1 { $profileType="Temporary" } 2 { $profileType="Roaming" } 4 { $profileType="Mandatory" } 8 { $profileType="Corrupted" } default { $profileType = "LOCAL" } } $User = $objUser.Value $ProfileLastUseTime = ([WMI]"").Converttodatetime($profile.lastusetime) $OutputObj = New-Object -TypeName PSobject $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.toUpper() $OutputObj | Add-Member -MemberType NoteProperty -Name ProfileName -Value $objuser.value $OutputObj | Add-Member -MemberType NoteProperty -Name ProfilePath -Value $profile.localpath $OutputObj | Add-Member -MemberType NoteProperty -Name ProfileType -Value $ProfileType $OutputObj | Add-Member -MemberType NoteProperty -Name IsinUse -Value $profile.loaded $OutputObj | Add-Member -MemberType NoteProperty -Name IsSystemAccount -Value $profile.special if($UserName) { if($User -match $UserName) { $OutputObj } } else { $OutputObj } } }
Output:
By default the script gets you all profiles in the computer. If you are looking for the details of a specific profile, just use the -UserName Parameter. For example, to get the profile of LocalUser1, use .\Get-UserProfiles.ps1 -UserName localuser1
Similarly to get the profiles on remote computer, use -ComputerName parameter. See below image for examples.
In my next post I will write about how to delete windows user profiles using powershell script and Win32_UserProfile WMI class.
Comments on this entry are closed.
Really a very good and especially helpful post! The script looks solid and I have to use it for various problem cases. Thank you Sitaram Pamarthi.
Hi,
can i get the script to delete user profile on the 2008 serverlocal or remote along with the sid delete in the registry with errorhandling.
Thank’s in advance
Manvi
Hi Manvi, See this
http://techibee.com/powershell/powershell-script-to-delete-windows-user-profiles-on-windows-7windows-2008-r2/1556
switch($status) {
or switch($profile.status) {
Dean, thanks for highlighting it. Corrected it to $Profile.Status
Is there a way to get the size of the profile folder in this script?
Hi, Measuring the size of profiles involves calculating the folder size remotely. So expect it to take long time. You may want to the following script.
https://gallery.technet.microsoft.com/scriptcenter/0f01d059-d951-40b0-b885-6b6b50384267
Great script! Is there a way to validate the results against Active Directory active user accounts?
FYI: I had to remove ($profile.lastusetime) from the line:
$ProfileLastUseTime = ([WMI]””).Converttodatetime($profile.lastusetime)
to stop an error from occurring on my Win7 PCs.
Thanks for the script. I am using it to display SIDs for users on a PC.