# autoFill description AD

**URL:** <https://forums.powershell.org/t/autofill-description-ad/19948>\
**Category:** PowerShell Help\
**Created:** [July 21, 2022, 1:47pm UTC](https://forums.powershell.org/t/autofill-description-ad/19948 "2022-07-21T13:47:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lounes\_Nait-atmane](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@Lounes\_Nait-atmane](https://forums.powershell.org/u/Lounes_Nait-atmane)\
**Post date:** [July 21, 2022, 1:47pm UTC](https://forums.powershell.org/t/autofill-description-ad/19948/1 "2022-07-21T13:47:58Z")

</div>

Hi all,

I would like to create a script that modifies the description field of the computers in my domain.

I want the ps1 file to query the AD in order to retrieve the last user (win2000) connected and the IP address and then register it in the description field of the computer object.

for exemple :

Script :

CHECK COMPUTER 1 =\> user “TATA” found, ip adresse : “10.0.0.1” found , copy the information and paste in description field of “Computer 1”

CHECK COMPUTER 2 =\> user “TOTO” found, ip …

I don’t want a script that runs on host machines that don’t have RSAT tools, I’ll run the script every 20 minutes

For moment i have this :

> Import-Module ActiveDirectory
> 
> # REMOTE COMPUTER
> 
> $ComputerName = ‘P5113931’
> 
> # OR
> 
> # LOCAL COMPUTER
> 
> #ComputerName = (Get-WmiObject -Class Win32\_ComputerSystem -Property Name).Name
> 
> $a = Get-Date  
> $b = $a.ToShortDateString()  
> $c = $a.ToShortTimeString()  
> $d = Get-WmiObject -computername $ComputerName Win32\_Computersystem | Select -Expand model
> 
> if ($d -eq $null) {$d = ‘0000’}  
> #$e = $d -replace ‘\D+(\d+)\D+’,‘$1’  
> $Description = “Damien script lounes \* $b $c \* $d”
> 
> Set-ADComputer $ComputerName -description $Description

i can modify one per one but is not my objectif.

Thank 🙂 a lot !

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 21, 2022, 6:01pm UTC](https://forums.powershell.org/t/autofill-description-ad/19948/2 "2022-07-21T18:01:18Z")

</div>

Lounès,  
Welcome to the forum. 👋🏽

> [@Lounes\_Nait-atmane](#):
>
> I want the ps1 file to query the AD in order to retrieve the last user (win2000) connected and the IP address and then register it in the description field of the computer object.

The AD does not have this information. You would need to query the eventlog of a computer to retrieve last logged on user.

> [@Lounes\_Nait-atmane](#):
>
> (win2000)

What does that mean? Are you really talking about Windows 2000? The support for Windows 2000 ended 12 years ago.

> [@Lounes\_Nait-atmane](#):
>
> CHECK COMPUTER 1 =\> user “TATA” found, ip adresse : “10.0.0.1” found , copy the information and paste in description field of “Computer 1”
> 
> CHECK COMPUTER 2 =\> user “TOTO” found, ip …
> 
> I don’t want a script that runs on host machines that don’t have RSAT tools, I’ll run the script every 20 minutes

Depending on your infrastructure that might not be the best idea for client computers. Ideally you already have a software deployment and metering solution in place? Or you could use GPOs to trigger certain tasks on the local computers. They don’t need to access the AD - they just should collect the needed information and send it to a central location.

```auto
Import-Module ActiveDirectory

```

Since PowerShell version 3 properly installed modules will be loaded automatically by PowerShell if needed. So no need to do this explicitly. 😉

```auto
$ComputerName = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name

```

That could be replaced by

```auto
$ComputerName = $ENV:COMPUTERNAME

```

And you should not use `Get-WmiObject` anymore. Instead use `Get-CimInstance`

> **[Get-CimInstance (CimCmdlets) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.2)**
>
> This cmdlet is only available on the Windows platform. The Get-CimInstance cmdlet gets the CIM instances of a class from a CIM server. You can specify either the class name or a query for this cmdlet. This cmdlet returns one or more CIM instance...

> [@Lounes\_Nait-atmane](#):
>
> i can modify one per one but is not my objectif.

If you want to run commands on more than one remote computer you can use loops. Either with

> **[about Foreach - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-7.2)**
>
> Describes a language command you can use to traverse all the items in a collection of items.

or

> **[ForEach-Object (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.2)**
>
> The ForEach-Object cmdlet performs an operation on each item in a collection of input objects. The input objects can be piped to the cmdlet or specified using the InputObject parameter. Starting in Windows PowerShell 3.0, there are two different ways...

or you could even pass an array of computernames to

> **[Invoke-Command (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.2)**
>
> The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote...

Please always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them.

In addition, the task you’re asking about is quite common and has been posted already several times throughout the internet. So you don’t need to re-invent the wheel again. You can just use what others already shared with hte world. 😉

And last but not least … When you post code, sample data, console output or error messages please format it as code using the _preformatted text_ button ( **\</\>** ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![Lounes\_Nait-atmane](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@Lounes\_Nait-atmane](https://forums.powershell.org/u/Lounes_Nait-atmane)\
**Post date:** [July 22, 2022, 3:10pm UTC](https://forums.powershell.org/t/autofill-description-ad/19948/3 "2022-07-22T15:10:05Z")

</div>

thank you for welcoming to this forum.

we already have tools that allow you to report information (GLPI in particular) but we want this script in addition (launched every two hours via scheduled task)

only administrator accounts can increment the AD, I want everything to be centralized on a server

my end goal would be to  
Uploading: image.png…  
Uploading: 1.png…

I trie this but is doesn’t work :

```auto
# Define the OU where the script will be applied
$NewComputers = Get-ADComputer -Filter * -SearchBase "OU=Computers,OU=xStaging OU,DC=company,DC=com"

# Define AD description
$ComputerDescription = (Get-WmiObject -Class Win32_OperatingSystem).Description

# Define attributes for computer description
foreach ($NewComputer in $NewComputers)
{
    # Update properties
    $NewComputer.Description = $ComputerDescription

    # Update the computer in AD
    $ADComputerName = Get-WmiObject -Class Win32_OperatingSystem -Property CSName
    if ($ADComputerName -eq $env:COMPUTERNAME) {Set-ADComputer -Instance $NewComputer}
}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 22, 2022, 3:35pm UTC](https://forums.powershell.org/t/autofill-description-ad/19948/4 "2022-07-22T15:35:30Z")

</div>

You did not read my answer and you did not read the help topics I posted. 🫤

> [@Lounes\_Nait-atmane](#):
>
> `$ComputerDescription = (Get-WmiObject -Class Win32_OperatingSystem).Description`

That’s querying the local computer for the description what’s in my experiences almost never filled in. Why do you want to use this property?

> [@Lounes\_Nait-atmane](#):
>
> `foreach ($NewComputer in $NewComputers)`

I’d recommend to choose variable names that are more distinct. Like `$ComputerName in $ComputerList`. But that’s just cosmetics. 😉

> [@Lounes\_Nait-atmane](#):
>
> `$NewComputer.Description = $ComputerDescription`

Here you fill in a variable for a computer from your list of computers and assign the description from the local computer you populated before **OUTSIDE** the loop!!

> [@Lounes\_Nait-atmane](#):
>
> `$ADComputerName = Get-WmiObject -Class Win32_OperatingSystem -Property CSName`

Here you query agan the LOCAL computer where the script runs.

> [@Lounes\_Nait-atmane](#):
>
> `if ($ADComputerName -eq $env:COMPUTERNAME)`

You compare the computername from the local computer determined by WMI with the computername from the local computer determined by environment variable. That’s in 99.9999 % of all cases true.

> [@Lounes\_Nait-atmane](#):
>
> ` {Set-ADComputer -Instance $NewComputer}`

To use the instance method for `Set-AdComputer` you have to query the particular computer from the AD first and change some of its properties. Please read the help including the examples - especially example 4!

> **[Set-ADComputer (ActiveDirectory)](https://learn.microsoft.com/en-us/powershell/module/activedirectory/set-adcomputer?view=windowsserver2022-ps)**
>
> Use this topic to help manage Windows and Windows Server technologies with Windows PowerShell.

> [@Lounes\_Nait-atmane](#):
>
> my end goal would be to  
> Uploading: image.png…  
> Uploading: 1.png…

… and please do not post images of code or error messages or console output. Instead post the plain text and format it as code.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/autofill-description-ad/19948/6 "2024-05-16T20:17:40Z")

</div>


