1

I have run the code below to gather information for all the machines on the network.

import-module activedirectory

$getget = Get-ADComputer -Filter "OperatingSystem -like 'Windows 10 *'" - Property * | select name -ExpandProperty Name 


echo $getget

foreach ($new1 in $getget) {


$bios = Get-WmiObject Win32_BIOS -ComputerName $new1
$Proc = Get-WmiObject Win32_processor -ComputerName $new1
$memory = Get-WmiObject Win32_physicalmemory -ComputerName $new1
$system = Get-WmiObject Win32_ComputerSystem -ComputerName $new1
$opera = Get-WmiObject win32_operatingsystem -ComputerName $new1
$ip = [System.Net.Dns]::GetHostAddresses("$new1")
$TotalSlots = (Get-WmiObject -Class "win32_PhysicalMemoryArray" - ComputerName $new1).MemoryDevices 


$Object = New-Object PSObject -Property @{

'OPerating system'     = $opera.Caption
IP                     = $ip.IPAddressToString 
UserName               = $system.UserName
ComputerName           = $proc.SystemName
Model                  = $system.Model
'Serial Number'        = $bios.SerialNumber
'Processor Name'       = $proc.name
'RAM (GB)'             = ($system.TotalPhysicalMemory / 1GB -as [int])
'Used RAM slot'        = $memory.count
'Total RAM slot'       = $totalslots}

Write-output $Object | Out-File "C:\web\.info.txt" -append
ECHO $object
}

The result is large text file with the format shown below, a block of text for each of the 100 odd machines on the network.

ComputerName     : MYMACHINE01
Processor Name   : Intel(R) Core(TM) XXXXXXXXXXXX
RAM (GB)         : 4
UserName         : 
IP               : xxx.xx.xxx.xxx
Total RAM slot   : 4
Used RAM slot    : 
OPerating system : Microsoft Windows 
Serial Number    : XXXXXXXXXXXX
Model            : OptiPlex XXXXXXX


ComputerName     : MACHINE02
Processor Name   : Intel(R) Core(TM) XXXXXXXXXXX
RAM (GB)         : 8
UserName         : XXXXXXXXXXX\XXXXXXX
IP               : XXX.XXX.XXX.XXX
Total RAM slot   : 2
Used RAM slot    : 2
OPerating system : Microsoft Windows 
Serial Number    : XXXXXXXXX
Model            : OptiPlex XXXXXXX

How can i change the code so that the labels on the left become the column titles and each machine becomes a row? e.g:

ComputerName, Processor Name, RAM (GB), UserName,       IP,      Total RAM slot, Used RAM slot, OPerating system, Serial Number,   Model    

MYMACHINE01   INTEL CORE      16        XXXXXXX  XXX.XXX.XXX.XXX       X                X         windows           XXXXXXXXX      XXXXX
MACHINE02     XXXXXXXXX        8        XXXXXXX  XXX.XXX.XXX.XXX       X                X         windows           XXXXXXXXX      XXXXX

I have tried with format-table but i had no success, as a powershell beginner i'm probably missing something simple. Any help with this would be greatly appreciated.

Thank you.

2
  • 1
    Write-output $Object | Out-File "C:\web\.info.txt" -append --> $Object | Export-Csv "C:\web\.info.txt" -NoTypeInformation -append Commented Mar 27, 2018 at 7:50
  • This was useful and helped me find a solution, thank you Commented Dec 18, 2019 at 15:01

1 Answer 1

1

You need to pipe the object to Format-Table before writing it to the file:

Write-Output $Object | Format-Table | Out-File "C:\web\.info.txt" -append
Sign up to request clarification or add additional context in comments.

5 Comments

thank you for this! it's the perfect format, but unfortunately it only writes 5 of the columns, and the last one IP is incomplete, any idea why this may be?
Because $ip.IPAddressToString returns an array. You either need to figure out which address you're interested in or flatten the array. You could do $ip.IPAddressToString -join ", " to put all addresses into a string for example
Ah ok, thank you. I'm only really interested in IPv4, would it be best to use another function to get this? Also, a similar issue with operating system, being cut-off, is this also an array output?
No, that is not an array. Don't know why that's getting cut, I can't replicate it. You could try Format-Table -AutoSize
Ok it worked!, many thanks! I had the powershell script running on my small (narrow) screen, for what ever reason moving it over to the widescreen monitor made it include all the missed-off columns, seems strange as it wasn't outputting to the screen at all but writing to a text file?? either way we managed, Thanks again :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.