Author Posts

March 30, 2016 at 11:40 am

Hello, below I have

$inactiveusers  = Search-ADAccount -AccountInactive -usersonly -TimeSpan 60 | % {$_.samaccountname}
#expired accounts
$expiredusers = Search-ADAccount -Accountexpired -UsersOnly | % {$_.samaccountname}
#disabled accounts
$disabledusers = Search-ADAccount -AccountDisabled -UsersOnly | % {$_.samaccountname}

#inactive computer accounts 60 days
$inactivecomputers = Search-ADAccount -AccountInactive -computersonly -TimeSpan 60 | % {$_.name}

#accounts that are disabled
$disabledcomputers = Search-ADAccount -AccountDisabled -computersOnly | % {$_.Name}

#select acounts older than 6 months with non-expiring passwords for validation
$nonexpiringPW = Get-ADUser –filter * -prop PasswordLastSet,passwordneverexpires | ?{$_.passwordlastset -lt ((Get-Date).AddDays(-180)) -and $_.passwordlastset -ne $null -and $_.passwordneverexpires} | % {$_.name}

How would I go about creating a table that would display each varible in it's own column?

inactive users expired users disabled computers....etc
——————– ——————- ———————–
bob1 tom1 b4324x-tc
blob1 tim2 asfdasdf-te

The columns aren't really related to eachother but it would mostly be just to consolidate the data in an easy to grab form.

March 30, 2016 at 12:04 pm

What you want to do is create a custom object. Typically you're not looking a table, but rather a collection of objects. Each row is an object and each "column header" is a property of the object. I'm not going to bother to work with your code, but let me give an example in which you can work.

$Computers = Get-ADComputer -Filter * -SearchBase "OU=TestOU,DC=mydomain,DC=com"
Foreach ($Computer in $Computers) {
    $ResolvedIP = Test-Connection -ComputerName $Computer.Name
    [pscustomobject]@{
        Name = $Computer.Name
        IsEnabled = $Computer.Enabled
        IP = $ResolvedIP.IPV4Address
    }
    $ResolvedIP = $null

To use this example, you'd really want to dump the $ResolvedIP each time you created a new object (which I included). Additionally, you'd want to add some error handling around the Test-Connection cmdlet (which I didn't include).

I'm sure there's a better example in which I could've shared. The point is this however, you collect various information about something, possibly from different sources and add those as properties to each custom object created by each loop. I've always liked this article: https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/07/a-powershell-object-lesson-part-3/ as it shows the different ways to create custom objects. I've used the PowerShell 3.0 (and greater) method.

Edit: I took a look at your code and my concern is that each object — again, each row — wouldn't be related. Think about Get-Service: each row is 100% related. You have a service's name, the same service's display name, and the same service's status. You might want to rethink what you want exactly... I'll keep thinking too.

March 30, 2016 at 12:52 pm

this can be improved, but it may work for your situation

$counts = $inactiveusers.Count, $expiredusers.Count, $disabledusers.count, $inactiveusers.Count, $disabledcomputers.count, $nonexpiringPW.count | sort -Descending

$bigtosmall = @()
foreach ($count in $counts) {
    switch ($count) {
        $inactiveusers.Count {$bigtosmall += '$inactiveusers'}
        $expiredusers.Count {$bigtosmall += '$expiredusers'}
        $disabledusers.Count {$bigtosmall += '$disabledusers'}
        $inactivecomputers.Count {$bigtosmall += '$inactivecomputers'}
        $disabledcomputers.Count {$bigtosmall += '$disabledcomputers'}
        $nonexpiringPW.Count {$bigtosmall += '$nonexpiringPW'}
    }
}

$result = New-Object System.Collections.ArrayList
1..$((iex $bigtosmall[0]).count) | % {$result.Add($null) | Out-Null}

foreach ($obj in $bigtosmall) {
    $obj = iex $obj
    for ($i = 0; $i -lt $obj.count; $i++) {
        $StringBuilder = New-Object System.Text.StringBuilder
        $StringBuilder.Append($result[$i]) | Out-Null
        $StringBuilder.Append(",$($obj[$i])") | Out-Null
        $result[$i] = $StringBuilder.ToString().Trim(',')
    }
}

$result | Out-File c:\temp\accounts.csv

March 31, 2016 at 4:58 am

It is sort of an odd request. Here is my stab at it.

# https://powershell.org/forums/topic/creating-a-table-with-multiple-variables/
# collect the data
[array]$inactiveusers  = Search-ADAccount -AccountInactive -usersonly -TimeSpan 60 | % {$_.samaccountname}
$iuCnt = $inactiveusers.Count
[array]$expiredusers = Search-ADAccount -Accountexpired -UsersOnly | % {$_.samaccountname}
$euCnt = $expiredusers.Count
[array]$disabledusers = Search-ADAccount -AccountDisabled -UsersOnly | % {$_.samaccountname}
$duCnt = $disabledusers.Count
[array]$inactivecomputers = Search-ADAccount -AccountInactive -computersonly -TimeSpan 60 | % {$_.name}
$icCnt = $inactivecomputers.Count
[array]$disabledcomputers = Search-ADAccount -AccountDisabled -computersOnly | % {$_.Name}
$dcCnt = $disabledcomputers.Count
[array]$nonexpiringPW = Get-ADUser –filter * -prop PasswordLastSet,passwordneverexpires | ?{$_.passwordlastset -lt ((Get-Date).AddDays(-180)) -and $_.passwordlastset -ne $null -and $_.passwordneverexpires} | % {$_.name}
$nxCnt = $nonexpiringPW.Count
Write-Verbose "$iuCnt, $euCnt, $duCnt, $icCnt, $dcCnt, $nxCnt"
[int]$max = ($iuCnt, $euCnt, $duCnt, $icCnt, $dcCnt, $nxCnt | Measure-Object -Maximum).Maximum
# create empty array to collect the data
$data =  New-Object -TypeName System.Collections.ArrayList

# put data into columns
for ($i = 0; $i -lt $max; $i++)
{
    # build out the one row
    $text =  "$($inactiveusers[$i]),"
    $text += "$($expiredusers[$i]),"
    $text += "$($disabledusers[$i]),"
    $text += "$($inactivecomputers[$i]),"
    $text += "$($disabledcomputers[$i]),"
    $text += "$($nonexpiringPW[$i])"
    # add row to the array
    $data.Add($text) | Out-Null
}

# several different options for output, this is just a few
$data | ConvertFrom-Csv -Header "inactiveusers","expiredusers","disabledusers", "inactivecomputers",
    "disabledcomputers","nonexpiringPW" | Out-GridView
$data | ConvertFrom-Csv -Header "inactiveusers","expiredusers","disabledusers", "inactivecomputers",
    "disabledcomputers","nonexpiringPW" | Export-Csv -Path .\data.csv -NoTypeInformation -Encoding ASCII

March 31, 2016 at 6:42 am

Looks great guys and I appreciate you helping out with what is really an odd request!

March 31, 2016 at 12:00 pm

Tightened up the above script a little.

# https://powershell.org/forums/topic/creating-a-table-with-multiple-variables/
$pwThreshold = (Get-Date).AddDays(-180)
# collect the data
$iuCnt = ([array]$inactiveUsers  = (Search-ADAccount -AccountInactive -usersonly -TimeSpan 60).samaccountname).Count
$euCnt = ([array]$expiredUsers = (Search-ADAccount -AccountExpired -UsersOnly).samaccountname).Count
$duCnt = ([array]$disabledUsers = (Search-ADAccount -AccountDisabled -UsersOnly).samaccountname).Count
$icCnt = ([array]$inactiveComputers = (Search-ADAccount -AccountInactive -computersonly -TimeSpan 60).Name).Count
$dcCnt = ([array]$disabledComputers = (Search-ADAccount -AccountDisabled -computersOnly).Name).Count
$nxCnt = ([array]$nonexpiringPW = (Get-ADUser –filter * -Properties PasswordLastSet, PasswordNeverExpires | 
    where {$_.PasswordLastSet -ne $null -and $_.PasswordLastSet -lt $pwThreshold -and 
    $_.passwordneverexpires}).Name).Count
Write-Verbose "$iuCnt, $euCnt, $duCnt, $icCnt, $dcCnt, $nxCnt"

# what's the length of the longest list?
[int]$max = ($iuCnt, $euCnt, $duCnt, $icCnt, $dcCnt, $nxCnt | Measure-Object -Maximum).Maximum

# create empty array to collect the data
$data = New-Object -TypeName System.Collections.ArrayList

# put data into columns
for ($i = 0; $i -lt $max; $i++)
{
    # build out the one row
    $text =  "$($inactiveUsers[$i]),$($expiredUsers[$i]),$($disabledUsers[$i]),"
    $text += "$($inactiveComputers[$i]),$($disabledComputers[$i]),$($nonexpiringPW[$i])"
    # add row to the array
    [void]$data.Add($text)
}

# several different options for output, this is just a few
$data | ConvertFrom-Csv -Header "Inactive Users","Expired Users","Disabled Users", "Inactive Computers",
    "Disabled Computers","Nonexpiring PW" | Out-GridView
$data | ConvertFrom-Csv -Header "Inactive Users","Expired Users","Disabled Users", "Inactive Computers",
    "Disabled Computers","Nonexpiring PW" | Export-Csv -Path .\data.csv -NoTypeInformation -Encoding ASCII