user66001
user66001

Reputation: 893

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

Upvotes: 0

Views: 1128

Answers (2)

CraftyB
CraftyB

Reputation: 746

Please see the below adjustments to your script.

You can pull the sub values of property by using the method GetValue.

I have also adjusted how you are storing each iteration and outputting the value post the foreach loop as the example above will just output each $profile as it was before the loop.

I have not tested with Add-RegKeyMember and therefore I am unable to confirm if this will pull the LastWriteTime property, but I can confirm that this will pull the profileimagepath property.

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

$ProfileData = @()

foreach($Profile in $Profiles){
    $ThisProfileInfo = $null

    $ThisProfileInfo = @{Name=$Profile.Name;
                 LastLogon=$Profile.GetValue("LastWriteTime");
                 ProfilePath=$Profile.GetValue("ProfileImagePath")}

    $ProfileData += $ThisProfileInfo
}

$ProfileData

Upvotes: 1

CFou
CFou

Reputation: 1180

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")

Upvotes: 1

Related Questions