Hi @Micro_Techie ,
Here we go with some logging:
$logfile = "C:\Temp\Logfile.log"
$OUs = Get-ADOrganizationalUnit -Filter 'Name -like "*"' -Properties * |
where {$_.ProtectedFromAccidentalDeletion -eq $true -AND $_.Name -match "TestOU"}
Out-File -FilePath $logfile -Encoding utf8 -InputObject "OUs found:"
Out-File -FilePath $logfile -Encoding utf8 -InputObject $OUs.DistinguishedName -Append
Out-File -FilePath $logfile -Encoding utf8 -InputObject "`r`nHere we go ...." -Append
foreach ($OU in $OUs)
{
$ouDN = $OU.DistinguishedName
try {
Set-ADOrganizationalUnit -identity "$ouDN" -ProtectedFromAccidentalDeletion $True
Out-File -FilePath $logfile -Encoding utf8 -InputObject "Value successfully set on OU: $OUDN" -Append
}
catch {
Out-File -FilePath $logfile -Encoding utf8 -InputObject "Something went wrong while setting value on OU: $OUDN" -Append}
}
Out-File -FilePath $logfile -Encoding utf8 -InputObject "... Done" -Append
----------
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
Regards
Andreas Baumgarten