Removing part of the Notes field

There will be occasions when you need to remove part of the Notes field

$ou = "OU=BlogTests,DC=Manticore,DC=org"            
            
$old = @"

Just a bit more info
"@             
            
"`nMicrosoft"            
$name = "UserA"            
$p = Get-ADUser -Identity $name -Properties *            
$note = ($p.info).Replace($old, "")            
Set-ADUser -Identity $name -Replace @{info = $note}            
            
"`nAD provider"            
$name = "UserB"            
$dn = "cn=$name,$ou"            
$p = Get-ItemProperty -Path AD:\$dn  -Name info             
$note = ($p.info).Replace($old, "")            
Set-ItemProperty -Path AD:\$dn  -Name info -Value $note  -Force            
             
"`nQuest"            
$name = "UserC"            
$p = Get-QADUser -Identity $name -IncludeAllProperties            
$note = ($p.info).Replace($old, "")            
Set-QADUser  -Identity $name -ObjectAttributes @{info=$note}            
            
"`nScript"            
$name = "UserD"            
$dn = "cn=$name,$ou"            
$user = [adsi]"LDAP://$dn"            
            
$note = @"
$(($user.info).ToString().Replace($old, ""))
"@            
$user.info = $note            
$user.SetInfo()


Create a here-string with the content you want to remove.



In all cases get the attribute value and use the String Replace() method to replace the unwanted text with an empty string “”



In the case of the script we substitute the result into a here-string for posting back.  Also notice how you have to use the ToString() method

Leave a Reply