Link to home
Start Free TrialLog in
Avatar of ndalmolin_13
ndalmolin_13Flag for United States of America

asked on

Using Powershell to find all PCs a user has logged into by checking to see if the user has a user profile

Hello Experts,

I need to find all of the computers within our domain that a particular user has logged into.  Unfortunately we don't have a log server from which I could query domain controller logs.  My thoughts are to query each PC to see if a user profile for the particular users exists on it.  The Powershell code I'm trying to use is as follows:
$User = read-host "Input the username of the individual that you want to search for"  
$SAMAccountName = get-aduser -Identity $user | Select samaccountname
$Computers = Get-AdComputer -filter {(Name -Like "dj*")} | select name | sort name

$Results = foreach ($Computer in $Computers)
{
    $CheckForProfile = "\\" + $Computer + "\c$\Users\" + $SamAccountName.samaccountname + "\"
    if((Test-Path $CheckForProfile))
    {
        $Computer
    }
}
$Results | Out-file "c:\work\Computers_$($SamAccountName.samaccountname)_has_logged_into.txt"

Open in new window

This is not returning any findings.  If I change the $Computers variable to a single PC (line 3) that I know my test account has logged into, it works.  What am I missing here?

Thanks in advance for your help.

Nick
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada 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

Try this:


$User = read-host "Input the username of the individual that you want to search for"  
$SAMAccountName = get-aduser -Identity $user | Select -ExpandProperty samaccountname
$Computers = Get-AdComputer -filter { (Name -Like "dj*") } | select -ExpandProperty name | sort 

$Results = foreach ($Computer in $Computers)
{
    $CheckForProfile = "\\" + $Computer + "\c$\Users\" + $SamAccountName + "\"
    if (Test-Path $CheckForProfile)
    {
        $Computer
    }
}
$Results | Out-file "c:\work\Computers_$($SamAccountName)_has_logged_into.txt"

Not sure the purpose.
One option for proactive, setup a user GPO that gas a login/logout script that records dates, time the user logs into and out of a computer system

Instead of searching for individual , why not just get the list of all user profiles on each system?

Depending on the setup, roaming profiles, folder redirection?
Avatar of ndalmolin_13

ASKER

Hello Mr. Johnson,

Doing computer.name did exactly what I needed it to do.  Thanks and sorry for the delay in getting a response to you.