This script will retrieve the LastUseTime of the user profile.
This information can be useful to determine logon statistics and general use patterns on systems.
The information is collected via WMI ( Win32_UserProfile ) and this class only works on Vista Sp1 and Server 2008 and later machines.
i.e. this will not work against XP or Server 2003
.EXAMPLE
"Server1","Server2" | Get-UserLastLogin
.EXAMPLE
Get-UserLastLogin -ComputerName Server1,Server2
Output:
PSComputerName UserDir LastUse LocalPath
-------------- ------- -------
---------
ServerTS12378 Administrator 1/3/2013 12:42:21 PM C:\Users\Administrator
ServerTS12378 User123
1/8/2013 12:43:44 PM C:\Users\user123
<#
.Synopsis
Retrieves the LastUseTime of the user profile
.DESCRIPTION
Retrieves the LastUseTime of the user profile using the WMI class Win32_UserProfile.
This class only works on: Server2008,VistaSp1+ machines.
.EXAMPLE
"Server1","Server2" | Get-UserLastLogin
.EXAMPLE
Get-UserLastLogin -ComputerName Server1,Server2
#>
function Get-UserLastLogin {
param(
[String[]]$ComputerName = $ENV:ComputerName
)
begin {
$NS = "root\CIMv2"
}
Process {
$ComputerName | ForEach-Object {
$Computer = $_
Get-WmiObject -Class Win32_UserProfile -Namespace $NS -ComputerName $Computer |
Where-Object {!$_.Special} |
ForEach-Object {
$UserDir = Split-Path -Path $_.LocalPath -Leaf
$Lastuse = $_.ConvertToDateTime($_.LastUseTime)
$_ | Add-Member -MemberType NoteProperty -Name UserDir -Value $UserDir
$_ | Add-Member -MemberType NoteProperty -Name LastUse -Value $Lastuse
$_ | Select-Object PSComputerName,UserDir,LastUse,LocalPath # ,sid, special
}
}#Foreach-Object(Computer)
}#Process
}#Get-UserLastLogin
<# .Synopsis Retrieves the LastUseTime of the user profile .DESCRIPTION Retrieves the LastUseTime of the user profile using the WMI class Win32_UserProfile. This class only works on: Server2008,VistaSp1+ machines. .EXAMPLE "Server1","Server2" | Get-UserLastLogin .EXAMPLE Get-UserLastLogin -ComputerName Server1,Server2 #> function Get-UserLastLogin { param( [String[]]$ComputerName = $ENV:ComputerName ) begin { $NS = "root\CIMv2" } Process { $ComputerName | ForEach-Object { $Computer = $_ Get-WmiObject -Class Win32_UserProfile -Namespace $NS -ComputerName $Computer | Where-Object {!$_.Special} | ForEach-Object { $UserDir = Split-Path -Path $_.LocalPath -Leaf $Lastuse = $_.ConvertToDateTime($_.LastUseTime) $_ | Add-Member -MemberType NoteProperty -Name UserDir -Value $UserDir $_ | Add-Member -MemberType NoteProperty -Name LastUse -Value $Lastuse $_ | Select-Object PSComputerName,UserDir,LastUse,LocalPath # ,sid, special } }#Foreach-Object(Computer) }#Process }#Get-UserLastLogin