How can I shorten/Clean up this extremely long line

$results += $computer | Select-Object name, dnshostname, operatingsystem,ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { tnc $_.ipv4address -InformationLevel Quiet }},@{ label = "Mac Address"; Expression={(Get-WmiObject win32_networkadapter -ComputerName $_.name).macaddress -ne $null}},@{ label = "HDD About to Fail"; Expression={Get-WmiObject -ComputerName $_.name -namespace root\wmi –class MSStorageDriver_FailurePredictStatus -ErrorAction Silentlycontinue |  Select  PredictFailure}} 

As-is, that’s probably the best you can do. If you wanted to break that down into more script form, you could use Add-Member to add one property at a time. You can also hit Enter after each comma in the property list, if you just want it more neatly formatted.

Cool, appreciate your time!

37mm,

It can’t really be made shorter, but there are many different ways to break it down and make it easier to read, troubleshoot and maintain. Here’s one way. Don and others will object to my use of backticks, but to me the benefits of the readability of the script outweigh any downside.

$PingResults    = $Null
$MacAddress     = $Null
$HDDAboutToFail = $Null

$PingResults    = tnc $_.ipv4address -InformationLevel Quiet
$MacAddress     = [boolean]( Get-WmiObject win32_networkadapter -ComputerName $_.name ).macaddress
$HDDAboutToFail = ( Get-WmiObject -ComputerName $_.name `
                                  -Namespace    root\wmi `
                                  -Class        MSStorageDriver_FailurePredictStatus `
                                  -ErrorAction  Silentlycontinue `
                  ).PredictFailure

$results += $computer | Select name,
                               dnshostname,
                               operatingsystem,
                               ipv4address,
                               lastlogondate,
                               logoncount,
                               @{ label = "PingResults"      ; Expression = { $PingResults    } },
                               @{ label = "Mac Address"      ; Expression = { $MacAddress     } },
                               @{ label = "HDD About to Fail"; Expression = { $HDDAboutToFail } }

Another approach on making it better readable.

$Properties = @(
    'name'
    'dnshostname'
    'operatingsystem'
    'ipv4address'
    'lastlogondate'
    'logoncount'
    @{
        label = 'PingResults' 
        Expression = { 
            tnc $_.IPv4Address -InformationLevel Quiet }
    }
    @{
        label = 'Mac Address'
        Expression={
            (Get-WmiObject Win32_NetworkAdapter -ComputerName $_.name).MacAddress -ne $null
        }
    }
    @{
        label = 'HDD About to Fail'
        Expression={
            Get-WmiObject -ComputerName $_.name -namespace root\wmi –class MSStorageDriver_FailurePredictStatus -ErrorAction Silentlycontinue | 
                Select-Object -Property PredictFailure
        }
    }
)
$results += $computer | Select-Object -Property $Properties

Backticks could be avoided with splatting inside the scriptblock {} of the expression.

$Properties = @(
    'name'
    'dnshostname'
    'operatingsystem'
    'ipv4address'
    'lastlogondate'
    'logoncount'
    @{
        label = 'PingResults' 
        Expression = { 
            tnc $_.IPv4Address -InformationLevel Quiet }
    }
    @{
        label = 'Mac Address'
        Expression={
            $Splat = @{
                ComputerName = $_.name
                Class = 'Win32_NetworkAdapter'
            }
            (Get-WmiObject @Splat).MacAddress -ne $null
        }
    }
    @{
        label = 'HDD About to Fail'
        Expression={
            $Splat = @{
                ComputerName = $_.name
                Namespace = 'root\wmi'
                Class = 'MSStorageDriver_FailurePredictStatus'
                ErrorAction = 'Silentlycontinue'
            }
            Get-WmiObject @Splat | 
                Select-Object -Property PredictFailure
        }
    }
)
$results += $computer | Select-Object -Property $Properties

Cool, exactly what i was looking for.

https://github.com/g60wall/Domain-Health-Reporting/blob/master/QuickADcomputerREPORT.ps1

Here is the product this produce