PowerShell for Windows Admins

Apr 23 2010   2:04PM GMT

User Profiles

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

 

Last time we looked at getting the currently logged on user. This time we’ll discover the user profiles that have been defined on our system.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
Get-WmiObject -Class Win32_UserProfile | 
foreach {
    $query =  “ASSOCIATORS OF {Win32_SID.SID=’” `
    + $_.Sid + “‘} WHERE ResultClass=Win32_SystemAccount”
     
    $trustee = Get-WmiObject -Query $query 
      
    if($trustee -eq $null){
        $query =  “ASSOCIATORS OF {Win32_SID.SID=’” `
        + $_.Sid + “‘} WHERE ResultClass=Win32_UserAccount”
        $trustee = Get-WmiObject -Query $query 
    }
    Add-Member -InputObject $_ -MemberType NoteProperty -Name User `
    -Value $($trustee.Caption) -PassThru
} |
Format-List User, Loaded, LocalPath, RoamingConfigured, RoamingPath,
RoamingPreference, Special, Status,
@{Name=“Last Download”; 
 Expression={$_.ConvertToDateTime($_.LastDownloadTime)}},
@{Name=“Last Upload”; Expression={$_.ConvertToDateTime($_.LastUploadTime)}},
@{Name=“Last Used”; Expression={$_.ConvertToDateTime($_.LastUseTime)}}

We use Win32_UserProfile and foreach we find the user account associated with the SID – this is lifted straight from our look at share permissions. We use the technique from last time to add a property to the current object only this time are using the pipeline object (originally from our WMI query).

We can then pipe into Format-List and perform our display including converting the dates.

This class only appears to be available on Windows Vista SP1 and above – see http://msdn.microsoft.com/en-us/library/ee886409(VS.85).aspx for details.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: