Solved

# How to save outpuit from powershell into *.csv

Posted on 2011-09-08
395 Views
I have following powershell scripts, where it will scan server name from AllServers.txt, around 300 servers. Do you gusy knwo how to construct powershell to save the output into csv format ? in the same C:\ data folder ?

foreach ($svr in get-content "C:\data\AllServers.txt") {$con = "server=$svr;database=master;Integrated Security=sspi"$cmd = "SELECT SERVERPROPERTY('ProductVersion') AS Version, SERVERPROPERTY('ProductLevel') as SP"
$da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)$dt = new-object System.Data.DataTable
$da.fill($dt) | out-null
$svr$dt | Format-Table -autosize
}
0
Question by:motioneye

LVL 7

Assisted Solution

celazkon earned 166 total points
ID: 36501707
Generally, to redirect the output to file instead of scripting console, use the out-file cmdlet.
For exact usage and info about it, see:
http://technet.microsoft.com/en-us/library/ee176924.aspx
0

LVL 10

Accepted Solution

wls3 earned 167 total points
ID: 36502147
Unless you format the object to work with the Export-Csv cmdlet (Get-Help Export-Csv -Examples), the Out-File cmdlet (Get-Help Out-File -examples), mentioned above, will be your more effective approach.
0

LVL 69

Assisted Solution

Qlemo earned 167 total points
ID: 36502293
Set-Location "C:\temp\ee"
$cmd = "SELECT SERVERPROPERTY('ProductVersion') AS Version, SERVERPROPERTY('ProductLevel') as SP"$set = new-object system.data.dataset
foreach ($svr in get-content "AllServers.txt") {$constr = "server=$svr;database=master;Integrated Security=sspi" (new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $constr)).Fill($set) | out-null
$obj =$set.Tables[0] | select-object @{n = 'Server'; e = {$svr}}, Version, SP }$obj | export-csv -NoTypeInformation "C:\temp\ee\AllServerVersions.csv"

Is one way to do that. \$obj would allow for further processing, if needed.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.