Link to home
Start Free TrialLog in
Avatar of MilesLogan
MilesLoganFlag for United States of America

asked on

Powershell - Pull date modified for profiles in C:\users

Hi EE

Anyone have a PS script or utility that I can use to pull the Date modified for all profiles in C:\Users for multiple servers ?
Avatar of Steven Harris
Steven Harris
Flag of United States of America image

You can use PowerShell with something like:

$wmi = [WMI] ""
$results = get-wmiobject Win32_UserProfile | foreach-object {
  $userAccount = [WMI] ("root/cimv2:Win32_SID.SID='{0}'" -f $_.SID)
  $userName = "{0}\{1}" -f $userAccount.ReferencedDomainName,$userAccount.AccountName
  new-object PSObject -property @{
    "Name" = $userName
    "LastUseTime" = $wmi.ConvertToDatetime($_.LastUseTime)
  }
}
$results | export-csv mycsv.csv -notypeinformation

Open in new window

Avatar of Qlemo
Of course that will only work locally. To apply it to a list of servers (and have improved output), you would need something like:
Get-WmiObject -ComputerName (get-content c:\Servers.txt) Win32_UserProfile | % {
  $userinfo = [WMI] ($_.__Path -Replace "Win32_UserProfile", "Win32_SID")
  New-Object PsObject -Property @{
    ComputerName= $_.__Server
    Domain      = $userinfo.ReferencedDomainName
    User        = $userinfo.AccountName
    LastUsed    = $_.ConvertToDatetime($_.LastUseTime)
  }
}

Open in new window

Avatar of MilesLogan

ASKER

Thank you both, I was able to get output from  ThinkSpaceSolutions for the local machine but I do need it for multiple machines so I was needing something like Qlemo's example ..

Qlemo please see the error below.


Get-WmiObject : Invalid class "Win32_UserProfile"
At C:\test\Qlemo_test.ps1:1 char:1
+ Get-WmiObject -ComputerName (get-content c:\test\Servers.txt) Win32_UserProfile  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Win32_UserProfile only to works with Vista/2008 and above.
my bad .. I was testing it to pull data from an XP machine .. worked great for 2003/2008 server.. thanks ! can you help me export it to a CSV file ?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
| export-csv -NoType c:\Results.csv  lol .. I was close !

thank you so much !