Adding text to current computer description

I can´t seem to get this script to work, it should add text if there are anything written in the description.
It somehow always overwrite whatever is there.
Any suggestions how i can fix this?
Thanks in advance \ Thomas

Import-Module activedirectory

Define the path to the file containing AD computer names

$computersToDisable = Import-Csv (Get-ChildItem “C:\temp\checked*.csv”)

Get the current date and time

$currentDateTime = Get-Date -Format “yyyy-MM-dd HH:mm:ss”

Loop through each computer name

foreach ($computer in $computersToDisable) {
# Get the current description of the computer
$currentDescription = (Get-ADComputer -Identity $computerName).Description

# Concatenate the new text with date and time to the current description
$newDescription = "$currentDescription`nDisabled on $currentDateTime"

# Set the new description for the computer
Set-ADComputer -Identity $computerName -Description $newDescription

}

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working). If you can’t see the </> in your toolbar, you will find it under the gear icon.

How to format code on PowerShell.org

This won’t work:

$currentDescription = (Get-ADComputer -Identity $computerName).Description

$currentDescription will always be null because the Description property is not returned by default. Change it to:

$currentDescription = (Get-ADComputer -Identity $computerName -Property Description).Description
1 Like

Thanks alot Matt.
The changes worked, but it opened up a bunch of new errors.
I´ll post back when i am done with hitting my head to the wall ::slight_smile: