Formatting Custom Object with Array

Hi Guys… I am running through an OU and pinging machines to see if they are ON then collecting the profiles from the computer. I am wanting to then email this information but I’m having some format issues.

Below is my code so you can see.

$object = @()



Foreach ($Computer in $Computers){

$DNSHostName = $Computer.DNSHostName

If (Test-Connection $DNSHostName -Count 1 -Quiet){

$Names = Get-ChildItem -Directory "\\$DNSHostName\C$\Users" -ErrorAction SilentlyContinue | Where {$_.Name -notlike "Public" -and $_.Name -notlike "Admin*"} -ErrorAction SilentlyContinue | Select Name

$line = [pscustomobject]@{

Hostname = $DNSHostName

Profiles = $Names

}

$object += $line

}

}

$object | Format-List

Basicly, I am getting a Array of profile names and this is causing the output to look like this

Hostname : Computer1.domain.local
Profiles : {@{Name=User.One}, @{Name=User.Two}}

Hostname : Computer2.domain.local
Profiles : {@{Name=User.One}, @{Name=User.Two}}

Is it possible to clean up the output of the profiles?

I’d prefer it to be like this, but I don’t really mind as long as its easy to read in an email.

Computer
Username1
Username2

Computer2
Username1
Username2

Can anyone suggest anything?

You could simply create a list if you want to have it as list. :wink:

Foreach ($Computer in $Computers){
$DNSHostName = $Computer.DNSHostName
If (Test-Connection $DNSHostName -Count 1 -Quiet){
’ ’
$DNSHostName
Get-ChildItem -Path “\$DNSHostName\C$\Users” -Directory -ErrorAction SilentlyContinue |
Where-Object {$.Name -notlike “Public” -and $.Name -notlike “Admin*”} -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty Name
}
}
You can pipe this to whatever you like

If you’re gathering profile information, you may want to consider WMI:

$results = Foreach ($Computer in $Computers | Select -ExpandProperty DNSHostName){
    If (Test-Connection $Computer -Count 1 -Quiet){

        Get-CimInstance -ClassName Win32_UserProfile -Filter "Special = 'False'" | 
        Select -Property LocalPath, 
                         SID,
                         LastUseTime,
                         @{Name='UserName';Expression={($_.LocalPath -split '\\')[2]}}
    }
}

If you are going to reach out to multiple computers and grab data, you might as well grab all the data you can. This would get the path, sid, lastusetime, username. You can get just the username, like so:

$results | Select Name, @{Name='UserName';Expression={($_.Profiles | Select -ExpandProperty UserName)}}

try this.

$object = @()
Foreach ($Computer in $Computers){
$DNSHostName = $Computer
If (Test-Connection $DNSHostName -Count 1 -Quiet){
$Names = (Get-ChildItem -Directory “\$Computer\C$\Users” -ErrorAction SilentlyContinue | Where {$.Name -notlike “Public” -and $.Name -notlike “Admin*”} -ErrorAction SilentlyContinue). Name

$line = [pscustomobject]@{
Hostname = $DNSHostName
Profiles = $Names }
$object += $line

}

} $object | Format-List

Try this:

 

$object = @()

Foreach ($Computer in $Computers){

$DNSHostName = $Computer.DNSHostName

If (Test-Connection $DNSHostName -Count 1 -Quiet){

$Names = Get-ChildItem -Directory "\\$DNSHostName\C$\Users" -ErrorAction SilentlyContinue | Where {$_.Name -notlike "Public" -and $_.Name -notlike "Admin*"} -ErrorAction SilentlyContinue | Select -expandproperty Name

Foreach ($Name in $Names)

{

$line = [pscustomobject]@{

Hostname = $DNSHostName

Profile = $Name

}

$object += $line

}

}

}

$object | Format-List