Powershell Array

I don’t know why, but I have a huge problem dealing with arrays in powershell, especially when I want to export the results into CSV. So this one is for me 🙂

The below script reads an input file for a list of server/computer objects. It then goes thru each line and does a gwmi call to pull the OS version (I built this script so that I could do a dump out of DNS and check each entry for the OS it was running).

Then it adds the results to an array, then adds THAT to another array since you can’t do an append on export-csv prior to PS 3.0. Then outputs it all in a nice format for you.

$erroractionpreference= "silentlycontinue"

$Inputfile=get-content c:\computers.csv
$out= @()


foreach ($Compname in $Inputfile){

$OSVer=gwmi win32_operatingsystem -computer $CompName

$OutObject= new-object psobject
 $outobject|add-member noteproperty Name $compname
 $outobject|add-member noteproperty OSVer $osver.name
 $out += $outobject


$out|export-csv $outfile -notypeinformation



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s