Simple PowerShell output formatting

Import-Module ActiveDirectory

Get-ADComputer -SearchBase 'OU=myou2,OU=myou1,dc=mydomain,dc=local' -Filter '*' | `

Select -Exp Name | %{echo $_ ; Get-WMIObject -Class Win32_BIOS -ComputerName $_| Format-List SerialNumber; `

$monitor = gwmi WmiMonitorID -Namespace root\wmi -computername $_;($monitor.SerialNumberID -notmatch 0 | foreach {[char]$_}) -join ""};

PowerShell neophyte here. I managed to hack this little line together from various scripts I found here and on other forums.

It works fine, but the output is messy. It appears with a lot of unnecessary linebreaks and the SerialNumber label:

COMPUTERNAME


SerialNumber : COMPUTERSERIAL



MONITORSERIAL

I'd like the output to simply be:

COMPUTERNAME
COMPUTERSERIAL
MONITORSERIAL

How would I go about this?

Thanks for reading.



August 21st, 2015 11:04am

Hello,

You can obtain it by storing data to object instead of retrieveing raw data.

The following script should produce your goals :

Import-Module ActiveDirectory; 
$Computers = Get-ADComputer -SearchBase 'OU=myou2,OU=myou1,dc=mydomain,dc=local' -Filter '*' | Select -Exp Name;


$Inventory = $Computers | %{
    $MonitorSerialNumber = $null;
    $(gwmi WmiMonitorID -Namespace root\wmi -ComputerName $_).SerialNumberID | %{[string]$MonitorSerialNumber += $_.toString() }
    New-Object -TypeName psobject -Property @{
        'computerName'=$_;
        'ComputerSerialNumber'= $(Get-WMIObject -ComputerName $_ -Class Win32_BIOS).SerialNumber;
        'MonitorSerialNumber'=$MonitorSerialNumber
    }
};

$Inventory | fl
  1. We import ActiveDirectory PowerShell Module and store a list of computers to assess ($computers).
  2. Foreach computer we retrieve monitorSerialNumber and store it in $MonitorSerialNumber variable
  3. Foreach computer we create an object with 3 properties : ComputerName,ComputerSerialNumber and MonitorSerialNumber.
  4. Once object created, we add it to $inventory which is a liste of objecte
  5. Finally we display $inventory as a list to obtain th following rsult

Regards,

Rgis

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 11:35am

Answered my own question:

Select-Object -expand

This cmd extracts the property without any formatting.

This works:

Import-Module ActiveDirectory

Get-ADComputer -SearchBase 'OU=myou2,OU=myou1,dc=mydomain,dc=local' -Filter '*' | `

Select -Exp Name | %{echo $_ ; Get-WMIObject -Class Win32_BIOS -ComputerName $_| Select-Object -expand SerialNumber; `

$monitor = gwmi WmiMonitorID -Namespace root\wmi -computername $_;($monitor.SerialNumberID -notmatch 0 | foreach {[char]$_}) -join ""


August 21st, 2015 11:41am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics