0

I have the following code:

$versionList = "Windows 2000","Windows XP","Windows Vista","Windows 7","Windows 8","Windows Server 2003","Windows Server 2008","Windows Server 2008 R2","Windows Server 2012","Windows Server 2012 R2"

for($i = 0;$i -ne 10;$i++){
    $versionCheck = Get-ADComputer -Filter * -Property * | select OperatingSystem | where {$_.OperatingSystem -match $versionList[$i]}
       if($i -eq 0){
           Write-Host "Clients:" -for Green
       }
       if($i -eq 5){
           Write-Host "Servers:" -for Green
       }
    Write-Host $versionList[$i] `t $($versionCheck.OperatingSystem).Count 
}

This gives the following output:

Clients:
Windows 2000     0
Windows XP   0
Windows Vista    0
Windows 7    0
Windows 8    0
Servers:
Windows Server 2003      0
Windows Server 2008      0
Windows Server 2008 R2   0
Windows Server 2012      1
Windows Server 2012 R2   0

The numbers for the Server based Operating systems line up fine. The clients not so much. I would normally format this into a table but as the variables get renewed every time the loop runs that would make a lot of tables. How would I go abouts to format this properly?

1
  • Did you try -ft option? Commented Oct 3, 2013 at 19:28

1 Answer 1

2

You can format your output by using string formatting like this:

Write-Host ("{0,-22} {1,5}" -f $versionList[$i],$($versionCheck.OperatingSystem).Count)
Sign up to request clarification or add additional context in comments.

2 Comments

Works like a charm. How does this work though? What does "{0,-22} {1,5}" -f indicate?
The first component between the curly braces references the parameter, 0 = $versionList[$i] and 1 = $($versionCheck.OperatingSystem).Count. The second component specifies the alignment (- = left alignment, + = right alignment). For more info, see: msdn.microsoft.com/en-us/library/txafckwd%28v=VS.90%29.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.