Foreach Loop Not Workign

I need Help i am a newbie every time i run this i get 0 for the count any help?

$OSLIST = “Windows 7”, “Windows Server 2012”, “Windows Server 2008”, “Windows XP”
$OUList = “Item1”, “Item2” # Example Items

Foreach($OU in $OUList) {

Foreach($OS in $OSLIST){

$Computers = @(Get-ADComputer -Properties Name,operatingSystem,lastLogontimeStamp -Filter {(OperatingSystem -like “$OS”)})| where DistinguishedName -Like “$OU
$OU
$OS
$Computers.Count
}

}

It’s better to filter the OU with the SearchBase. In your example, you are performing 8 queries against Get-ADComputer, one for each OS and one for each OU. Additionally, you are saying Get ALL computers for an OS and then pass them through the pipeline to filter by OU.

Consider consolidating all of your OS’s into a single query. Filter your OU’s by using Get-ADOrganizationalUnit and then we tell Get-ADComputer to only get computers from that OU. To keep things neat and readable, I’m also using HereString and parameter Splatting. This isn’t tested code, but should get you close to what you are trying to accomplish. This would perform 1 query for each OU, so 2 queries and they are only returning what you want.

#HereString
$osFilter = @"
OperatingSystem -like '*Windows XP*'   
-or
OperatingSystem -like '*Windows 7*' 
-or
OperatingSystem -like '*Windows Server 2008*' 
-or
OperatingSystem -like '*Windows Server 2012*' 
"@

$computers = foreach($OU in (Get-ADOrganizationalUnit -Filter "Name -like 'User*'")) {
    #Splatting
    $splat = @{
        Properties =  @("Name","operatingSystem","lastLogontimeStamp")
        Filter = $osFilter
        SearchBase = $OU.DistinguishedName
    }

    Get-ADComputer @splat
}

$computers.Count

Hey so i tried what you showed me. Except i changed Get-ADOrganizationalUnit to Get-Adobject.

But i wanted to get a list count of each OS in Each OU. I only get a count of All The OS in one OU.

Function Get-OSLIST2{
[CmdletBinding()]
#HereString

$osFilter = @"
OperatingSystem -like ‘Windows XP
-or
OperatingSystem -like ‘Windows 7
-or
OperatingSystem -like ‘Windows Server 2008
-or
OperatingSystem -like ‘Windows Server 2012
"@

$computers = foreach($OU in (Get-ADObject -Filter “Name -like ‘Appleton’”)) {
#Splatting
$splat = @{
Properties = @(“Name”,“operatingSystem”,“lastLogontimeStamp”)
Filter = $osFilter
SearchBase = $OU.DistinguishedName
}

Get-ADComputer @splat

}
$OU.Name

$computers.Count

}

   Function Get-OSLIST2{
[CmdletBinding()]
#HereString



$osFilter = @"
OperatingSystem -like '*Windows XP*'
-or
OperatingSystem -like '*Windows 7*'
-or
OperatingSystem -like '*Windows Server 2008*'
-or
OperatingSystem -like '*Windows Server 2012*'
"@

$computers = foreach($OU in (Get-ADObject -Filter "Name -like 'Appleton'")) {
    #Splatting
    $splat = @{
        Properties =  @("Name","operatingSystem","lastLogontimeStamp")
        Filter = $osFilter
        SearchBase = $OU.DistinguishedName
    }

    Get-ADComputer @splat
}
$OU.Name

$computers.Count


}



 

You can get a count of each OS using Group-Object:

function Get-OSList{
    [CmdletBinding()]
    param()

    begin {
#Do not indent the here string
$osFilter = @"
OperatingSystem -like '*Windows XP*'
-or
OperatingSystem -like '*Windows 7*'
-or
OperatingSystem -like '*Windows Server 2008*'
-or
OperatingSystem -like '*Windows Server 2012*'
"@
    }
    process {
        $computers = foreach($OU in (Get-ADObject -Filter "Name -like 'Appleton'")) {
            Write-Verbose ("Performing query for OU: {0}" -f $OU.Name)
            #Splatting
            $splat = @{
                Properties =  @("Name","operatingSystem","lastLogontimeStamp")
                Filter = $osFilter
                SearchBase = $OU.DistinguishedName
            }

            Get-ADComputer @splat
        }
    }
    end {
        $computers | Select *
    }

}

$osList = Get-OSList
$osList | Group-Object -Property OperatingSystem | Sort-Object Count -Descending

That worked For me Huge Thanks.