1

Since, i am a beginner, i 've no much hands-on to the powershell programming.Although i had a script developed to insert data from an array to the csv file as follows:

#Following is the array
$InventoryReport = New-Object -TypeName PSobject -Property @{
                        ComputerName = "1myComputerName"
                        DomainName = "2myComputerDomain"
                        Manufacturer = "3myComputerManufacturer"
}

#Now to export the data to csv, i am using following:
$InventoryReport |Select-Object -Property ComputerName, DomainName, Manufacturer | Export-Csv -Path "c:\abc.csv" -NoTypeInformation -ErrorAction Stop

#This works fine

and the output of above is :

"ComputerName","DomainName","Manufacturer" "1myComputerName","2myComputerDomain","3myComputerManufacturer"

.... Now, i don't want this , i want the ouput to appear in columnar fashion i.e.

"ComputerName","1myComputerName"
"DomainName","2myComputerDomain"
"Manufacturer","3myComputerManufacturer"

What code changes should be done to achieve this. ?

0

2 Answers 2

1

Either you want CSV, which you already have, or you want a custom txt-file. If you want the latter, try this:

$comp = gwmi win32_computersystem

@"
"ComputerName","$($comp.Name)"
"DomainName","$($comp.Domain)"
"Manufacturer","$($comp.Manufacturer)"
"@ | Out-File test.txt

sample of test.txt output below. I've got a non-domain, custom built pc, so don't worry about the values.

"ComputerName","GRAIMER-PC"
"DomainName","WORKGROUP"
"Manufacturer","System manufacturer"

EDIT I suggest you learn what CSV is. Remember that CSV is not a fileformat, it's a formatting-style used in a normal textfile. The .csv extension is just cosmetic to let people know that the textfile uses the csv-style. Check out Wikipedia and Technet

In the CSV file, each object is represented by a comma-separated list of the property values of the object. The property values are converted to strings (by using the ToString() method of the object), so they are generally represented by the name of the property value. Export-CSV does not export the methods of the object.

The format of an exported file is as follows:

-- The first line of the CSV file contains the string '#TYPE ' followed by the fully qualified name of the object, such as #TYPE System.Diagnostics.Process. To suppress this line, use the NoTypeInformation parameter.

-- The next line of the CSV file represents the column headers. It contains a comma-separated list of the names of all the properties of the first object.

-- Additional lines of the file consist of comma-separated lists of the property values of each object.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Graimer, This solution looks, very staright forward. but i am looking for and export-csv to be used for genrating the csv file in a columnar fashion.
@Graimer has provided you with exactly what you asked for. It's name/value pairs, one per line, separated by commas, with quotes around the text.
@Ambrish I'm not trying to be rude, BUT YOU'RE NOT LISTENING. CSV's format is one row per object with properties/columns seperated by a delimiter (comma is default). I've added some links and quotes from wikipedia and technet's documentation for export-csv.
1

You could try something like this:

$InventoryReport | Format-List ComputerName, DomainName, Manufacturer `
  | Out-String -Stream `
  | ? { $_ -ne '' } `
  | % { $_ -replace '\s+:\s+', '","' -replace '(^|$)', '"' }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.