Issues with get-content inside select-object

I have a share containing a text file per machine with the name of the last logged on user. I wanted to add that information to my search. The get-content works fine when running alone. It is only returning a single string as checked with trim and length. What am I doing wrong?


Get-ADComputer -Filter {operatingsystem -like “7” -and enabled -eq $true} -Properties Name,Operatingsystem,lastlogondate,distinguishedname |
Select-Object Name,OperatingSystem,LastLogonDate,distinguishedname,@{name=‘User’;Expression={
Get-Content -Path "\server\share\computers$($.name).txt" -Tail 1 }} |
Where-Object {$
.LastlogonDate -gt $((Get-date).AddDays(-30))}



Try this.

Get-ADComputer -Filter {operatingsystem -like "*7*" -and enabled -eq $true} -Properties Name,
Operatingsystem,lastlogondate,distinguishedname |
Where-Object {$_.LastlogonDate -gt $((Get-date).AddDays(-30))} | 
Select-Object Name,OperatingSystem,LastLogonDate,distinguishedname,
@{name='User';Expression={Get-Content -Path "\\server\share\computers\$($" -Tail 1 }}

Thanks for your reply but I still have the same issue. For some reason the get-content part will not run inside of the select object.

What exactly does that mean? Do you get an error? Do you get nothing at all? Did you try to output the path you’re building just to check if it is what you expect. Do these paths really exist? “… will not run …” is pretty unprofessional. :wink:

Sorry for the short answer I agree it was not very informative. If I run the get-content command on its own I get the results supplementing the computername for a chosen device that would of appeared in the results. However when I run it combined the user field is blank. I have tried running trim and length commands and the string returned by the single command should be easily displayed.

What’s an example of the output you’re getting? It seems like it should work.

echo 'one
two' > joe.txt

[pscustomobject]@{name='joe'; lastlogondate=$((Get-date).AddDays(-29))} | 
select-object lastlogondate,
  @{name='user';e={get-content -path "$($" -tail 1}} | 
Where-Object {$_.LastlogonDate -gt $((Get-date).AddDays(-30))}

lastlogondate      user
-------------      ----
7/18/19 8:28:46 AM two