Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Exporting to CSV and XML


In this recipe, we pipe the results of the Get-Process cmdlet to a CSV and XML file.

How to do it...

These are the steps required to export to CSV and XML:

  1. Open PowerShell ISE as an administrator.

  2. Add the following script and run it:

    $csvFile = "C:\Temp\sample.csv"
    Get-Process | 
    Export-Csv -path $csvFile -Force -NoTypeInformation
    
    #display text file in notepad
    notepad $csvFile 
    
    
    $xmlFile = "C:\Temp\process.xml"
    Get-Process | 
    Export-Clixml -path $xmlFile  -Force
    
    #display text file in notepad
    notepad $xmlFile 

How it works...

PowerShell provides a few cmdlets that support exporting data to files of different formats. The Export-Csv cmdlet saves information to a comma-separated value file, and the Export-Clixml cmdlet exports the piped data to XML:

$csvFile = "C:\Temp\sample.csv"
Get-Process |
Export-Csv -path $csvFile -Force -NoTypeInformation

#display text file in notepad
notepad $csvFile 


$xmlFile = "C:\Temp\process.xml"
Get-Process |
Export-Clixml -path $xmlFile...