question

AlexRothwell-9481 avatar image
0 Votes"
AlexRothwell-9481 asked IanXue-MSFT answered

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

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-server-powershell
image.png (156.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.