Techibee.com

Powershell: Script Get Windows User Profiles on Remote Computer

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.

Exit mobile version