Chrismo16
Chrismo16

Reputation: 17

Filter Powershell Script for Enabled Computer Accounts Odd Results

Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 359.00:00:00 -Searchbase "OU=Disabled Computers,DC=mydomain,DC=net"

When filtering the previous script with Where {$_.enabled -eq 'false'} I get no results.

But when filtering with Where {$_.enabled -ne 'true'} I get the expected results.

Anyone know why this would be??

Upvotes: 0

Views: 105

Answers (1)

BenH
BenH

Reputation: 10044

This is because the enabled property isn't the string False. But is a Boolean value that gets formatted as a string when displayed. For example, if you evaluate $false you'll receive back what looks like the string False. But if you evaluate $False | Get-Member you will see that TypeName: System.Boolean

That means that you should compare to $False rather than "False"

$InactiveComputers = Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 359.00:00:00 -Searchbase "OU=Disabled Computers,DC=mydomain,DC=net"
#Displaying that enabled property is System.Boolean
$InactiveComputers[0].enabled | Get-Member
$InactiveComputers | Where {$_.enabled -eq $False}

Edit:

Alternatively, by switching the order of the compare you can dynamically cast $False to the string. This is the difference between "False" -eq $False and $False -eq "False"

$InactiveComputers = Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 359.00:00:00 -Searchbase "OU=Disabled Computers,DC=mydomain,DC=net"
$InactiveComputers | Where {"False" -eq $_.enabled}

Upvotes: 1

Related Questions