Powershell - Active Directory - Writing currently logged on user into computer description box

Alex Rothwell 1 Reputation point
2021-03-08T17:05:33.117+00:00

Hi there, first post here :)

I am wanting to write a script so that the last logged on user of a computer (more specifically, the Display Name) gets written into the computer Description of the computer that they are currently logged into.

For example:

Find the PC name
On that PC, find the currently logged on user, and find the Display Name
Then, on that PC, in the description in AD, write the Display Name of the currently logged in user

75447-image.png

I hope that made sense

Many thanks

Alex Rothwell

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-03-09T05:21:09.97+00:00

    Hi,

    Does this work for you?

    $server = 'W10GB54'  
    $user = query user /server:$server |ForEach-Object {  
            ($_ -split '  '| Where-Object {$_}) -join ','} |   
            ConvertFrom-Csv | Select-Object -ExpandProperty USERNAME  
    $displayname = Get-ADUser -Filter {Name -eq $user} -Properties DisplayName | Select-Object -ExpandProperty DisplayName  
    Set-ADComputer -Identity $server -Description $displayname   
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.