Powershell Append text to object description in Active Directory

How can I append or prepend the description in AD I want to leave the current description and put some text infront of it

for example a computer has the description as "Accounting Dept" (without quotes)

I tried this:

set-QADComputer -Identity computername
    -Description {Disabled 8/17/2012, Termrpt "$($_.description)"}

I get this for the description

Disabled 8/17/2012, "$($_.description)"

but I want the orginal description prepended by the text like the following

Disabled 8/17/2012, Accounting dept

any ideas?

I tried parentheses instead but then it just puts the prepended text and wipes out the original altogether.


Solution 1:

I don't use the QWEST AD cmdlets, so I don't know the exact syntax, but generally the best way is to retrieve the current description, save it to a variable, and then just write $Current_Desc + $addendum back to the object.

Solution 2:

I don't use the Qwest modules. If you're willing to use the Microsoft AD module, included with RSAT, the following is pretty straightforward.

Import-Module ActiveDirectory

# Let's check the Description
Get-ADUser jscott -Properties Description |
  Select-Object -Property Description

Description
-----------
Junior Keyboard MRO Tech

# Cool, set it the new value
Get-ADUser jscott -Properties Description |
  ForEach-Object {
    Set-ADUser $_ -Description "Disabled 8/17/2012, Termrpt $($_.Description)"
  }

# Let's check the new Description
Get-ADUser jscott -Properties Description |
  Select-Object -Property Description

Description
-----------
Disabled 8/17/2012, Termrpt Disabled Junior Keyboard MRO Tech

I think what's tripping you up is the usage of $_ as parameter of a cmdlet, rather than within a script block. I've wrapped Set-ADUser in a ForEach_Object, ensuring $_ is the object from the pipeline. Outside of a script block, as in your case, using $_ as a parameter will return $null.