which powershell conditions can be used for below scenario

%3CLINGO-SUB%20id%3D%22lingo-sub-3013562%22%20slang%3D%22en-US%22%3Ewhich%20powershell%20conditions%20can%20be%20used%20for%20below%20scenario%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3013562%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EWe%20need%20to%20tag%20all%20the%20computers%20under%20ABC.com%2F_AB%2FWorkstations%2FSGV%2FADNWE.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%22HAW%22%20needs%20to%20be%20written%20to%20extensionattribute1%20on%20computer%20objects.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EOnce%20the%20computer%20is%20moved%20out%20of%20this%20OU%20the%20tag%20must%20be%20removed.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3013562%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3057874%22%20slang%3D%22en-US%22%3ERe%3A%20which%20powershell%20conditions%20can%20be%20used%20for%20below%20scenario%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3057874%22%20slang%3D%22en-US%22%3ETo%20add%2C%20you%20could%20do%20a%20search%20for%20all%20computers%20in%20the%20ADNWE%20OU.%20Something%20like%3CBR%20%2F%3E%24computers%3Dget-adcomputer%20-filter%20*%20-properties%20*%20%7C%20where-object%20extensionattribute%20-ne%20'HAW'%20%7C%20where-object%20distinguishedname%20-match%20'ou%3Dadnwe%2Cou%3Dsgv%2Cou%3Dworkstations%2Cou%3D_ab%2Cdc%3Dabc%2Cdc%3Dcom%3CBR%20%2F%3Eforeach%20(%24computer%20in%20%24computers)%20%7B%3CBR%20%2F%3Eset-adcomputer%20%24computer%20-add%20%40%7BExtensionAttribute1%3D%22HAW%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3ETo%20remove%2C%20you%20could%20do%20a%20search%20for%20all%20computers%20with%20a%20extensionattribute1%20filled%20with%20HAW%20which%20is%20not%20present%20in%20the%20ADNWE%20OU.%20Something%20like%3CBR%20%2F%3E%24computers%3Dget-adcomputer%20-filter%20*%20-properties%20*%20%7C%20where-object%20extensionattribute%20-eq%20'HAW'%20%7C%20where-object%20distinguishedname%20-notmatch%20'ou%3Dadnwe%2Cou%3Dsgv%2Cou%3Dworkstations%2Cou%3D_ab%2Cdc%3Dabc%2Cdc%3Dcom%3CBR%20%2F%3Eforeach%20(%24computer%20in%20%24computers)%20%7B%3CBR%20%2F%3Eset-adcomputer%20%24computer%20-Clear%20%22extensionAttribute1%22%3CBR%20%2F%3E%7D%3C%2FLINGO-BODY%3E
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?