which powershell conditions can be used for below scenario

Frequent Visitor

We need to tag all the computers under ABC.com/_AB/Workstations/SGV/ADNWE.

"HAW" needs to be written to extensionattribute1 on computer objects.

Once the computer is moved out of this OU the tag must be removed.

3 Replies

To add, you could do a search for all computers in the ADNWE OU. Something like
$computers=get-adcomputer -filter * -properties * | where-object extensionattribute -ne 'HAW' | where-object distinguishedname -match 'ou=adnwe,ou=sgv,ou=workstations,ou=_ab,dc=abc,dc=com
foreach ($computer in $computers) {
set-adcomputer $computer -add @{ExtensionAttribute1="HAW}
}

To remove, you could do a search for all computers with a extensionattribute1 filled with HAW which is not present in the ADNWE OU. Something like
$computers=get-adcomputer -filter * -properties * | where-object extensionattribute -eq 'HAW' | where-object distinguishedname -notmatch 'ou=adnwe,ou=sgv,ou=workstations,ou=_ab,dc=abc,dc=com
foreach ($computer in $computers) {
set-adcomputer $computer -Clear "extensionAttribute1"
}

Does this work for you in your environment?

Do you have SCCM in your environment?
If so, you can take a collections based approach for clearing the ext attribute if a computer has moved out of the required OU

@vickynet Did one of our answers help you?