Due to the nature of the operations, it’s not straightforward to combine these into a single query. The checking of WriteAccountRestrictions is dependent on the results of the first query (list of all enabled computers).
Here’s a way to write it as a single script that performs both operations:
# Get all enabled computers
$allEnabledComputers = Get-ADComputer -Filter "Enabled -eq 'True'" | Select-Object -ExpandProperty Name
# Initialize an empty array to hold computers with allowed WriteAccountRestrictions
$writeAllowEnabledList = @()
# Check each computer
foreach ($eachComputername in $allEnabledComputers) {
# Get the computer's security descriptor
$securityDescriptor = (Get-ADComputer -Filter "Name -like '$eachComputername'" -Properties nTSecurityDescriptor | Select-Object -ExpandProperty nTSecurityDescriptor).Access
# Check if 'Everyone', 'Domain Users', or 'Users' have Allow WriteAccountRestrictions
if ($securityDescriptor | Where-Object { $_.IdentityReference -in 'Everyone', 'Domain Users', 'Users' -and $_.ActiveDirectoryRights -eq 'WriteAccountRestrictions' -and $_.AccessControlType -eq 'Allow' }) {
# If so, add the computer to the list
$writeAllowEnabledList += $eachComputername
}
}
# Output the list of computers with allowed WriteAccountRestrictions
$writeAllowEnabledList