Search code examples
powershellregistryget-childitem

How to access properties of Get-Child Registry output


Using the below code, I get Name & LastLogon populated, but not ProfilePath.

Add-RegKeyMember is https://gallery.technet.microsoft.com/scriptcenter/Get-Last-Write-Time-and-06dcf3fb .

I have tried to access ProfileImagePath with $Profile.Properties.ProfileImagePath, $Profile.Name.ProfileImagePath, and others, but they all return blank (could be null). How on earth is this seemingly object making these properties available?

$Profiles = get-childitem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Add-RegKeyMember

foreach($Profile in $Profiles)
{

  $ThisProfileInfo = @{Name=$Profile.Name;
                     LastLogon=$Profile.LastWriteTime;
                     ProfilePath=$Profile.ProfileImagePath}
  $Profile
}

Name                           Property                                                                                                                                                       
----                           --------                                                                                                                                                       
S-1-5-18                       Flags            : 12                                                                                                                                          
                               ProfileImagePath : C:\WINDOWS\system32\config\systemprofile                                                                                                    
                               RefCount         : 1                                                                                                                                           
                               Sid              : {1, 1, 0, 0...}                                                                                                                             
                               State            : 0

Solution

  • This is because [Microsoft.Win32.RegistryKey] object returns properties as string array. You should simply retrieve the value ProfileImagePath from the object itself :

    ProfilePath=$Profile.GetValue("ProfileImagePath")