Issues Finding last logged on user

Just started a new job, and the previous guy was really bad about updating our documentation. We are trying to get that fixed. I was trying to start with figuring out who has what computer. Initially I was just checking the last modified user file. However, I was getting an abnormally high amount of computers just not answering.

I came up with the script below next. If I run the script on a specific computer, it works, but I can’t get it to work right when pulling from the CSV. Computers that I know are on, just don’t answer, or it says it can’t find the path. I’m still brand new to using powershell, and have run out of ideas. Any help would be appreciated.

$computers = import-csv "C:\temp\computerlist.csv"</code>
<code>$outputfile = "C:\temp\Systems-With-Last-User.txt"</code>
<code>foreach ($computer in $computers){</code>
<code>$computername = $computer.'computername'</code>
<code>write-host "Begin $computername"</code>
<code>add-content $outputfile "Begin $computername"</code>
<code>Get-Service -ComputerName $computername -Name RemoteRegistry | Set-Service -StartupType Manual</code>
<code>Get-Service -ComputerName $computername -Name RemoteRegistry | start-Service</code>
<code>REG QUERY \\$computername\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI /v LastLoggedOnUser | out-file -append $outputfile -Encoding utf8</code>
<code>add-content $outputfile "End $computername"</code>
<code>Get-Service -ComputerName $computername -Name RemoteRegistry | Stop-Service</code>
<code>write-host "End $computername"</code>