
Exporting, Importing, an d Converting Pages Reusable content Files Object Data
PowerShell provides a variety of cmdlets for exporting, importing, and converting object data into different formats, such as CSV, XML, JSON, and HTML. These tools allow you to save, share, and manipulate data in a structured way, making it easier to work with external tools or systems.
1. Exporting and Importing Data
Export-CSV and Import-CSV
The Export-CSV cmdlet saves object data into a Comma-Separated Values (CSV) file. Each object becomes a row, and its properties become the column headers. The Import-CSV cmdlet reads the CSV file and recreates the objects.
Example:
# Export services to a CSV file
Get-Service | Export-CSV -Path services.csv
# Import the CSV file
$data = Import-CSV -Path services.csv
# Filter imported data
$data | Where-Object { $_.Status -eq 'Running' }Key Points:
CSV files are lightweight and easy to work with in tools like Excel or databases.
CSV does not support complex, nested objects (unlike XML or JSON).
Export-CLIXML and Import-CLIXML
The Export-CLIXML cmdlet saves object data into an XML file, preserving the object's type and properties. The Import-CLIXML cmdlet recreates the objects from the XML file.
Example:
# Export services to an XML file
Get-Service | Export-CLIXML -Path services.xml
# Import the XML file
$objects = Import-CLIXML -Path services.xmlKey Points:
XML files preserve complex, nested object structures.
Use the
-Depthparameter to control how many layers of nested objects are exported.
ConvertTo-JSON and ConvertFrom-JSON
The ConvertTo-JSON cmdlet converts objects into JSON format, which is commonly used in web applications. The ConvertFrom-JSON cmdlet recreates objects from JSON data.
Example:
# Convert services to JSON and save to a file
Get-Service | ConvertTo-JSON | Out-File -FilePath services.json
# Read JSON file and convert back to objects
$object = Get-Content -Raw services.json | ConvertFrom-JSONKey Points:
JSON is a lightweight format, ideal for web APIs and modern applications.
Use the
-Depthparameter to handle nested objects.Use the
-Compressparameter to reduce file size by removing whitespace.
2. Comparing CSV, XML, and JSON
Format
Strengths
Weaknesses
CSV
Lightweight, easy to read/edit in Excel or text editors.
Does not support nested objects.
XML
Preserves complex object structures. Supports XPath queries.
Verbose and harder to read/edit manually.
JSON
Lightweight, widely used in web applications.
Less human-readable when compressed.
When to Use:
CSV: For simple data or when working with spreadsheets.
XML: For complex, nested object structures.
JSON: For web APIs or modern applications.
3. ConvertTo-HTML
The ConvertTo-HTML cmdlet converts object data into an HTML file, which can be viewed in a web browser or hosted on a web server.
Example:
# Generate an HTML report of processes
Get-Process |
ConvertTo-HTML -Property Name, Path, ID `
-Title "Process Report" `
-Head "<h1>Process Report</h1>" `
-Body "<h2>Report Was Run: $(Get-Date)</h2><hr>" |
Out-File -FilePath $env:TEMP\report.html
# Open the HTML file in the default browser
Invoke-Item $env:TEMP\report.htmlKey Points:
HTML is ideal for creating human-readable reports.
Use the
-Propertyparameter to specify which object properties to include.
4. Output Cmdlets
PowerShell provides several cmdlets to control how data is displayed or saved:
Out-Default
Automatically pipes output to the default formatter and outputter.
Used when no explicit outputter is specified.
Out-Host
Displays output in the console.
Supports the
-Pagingparameter to show output one page at a time.
Example:
Get-ChildItem $env:WINDIR | Out-Host -Paging
Out-File
Saves output to a file.
Supports parameters like
-Encoding,-Append, and-Force.
Example:
Get-Alias | Out-File C:\aliases.txt -ForceOut-GridView
Displays output in a graphical table.
Use the
-PassThruparameter to filter and pass selected objects to the next command.
Example:
Get-Service | Out-GridView -PassThru | Stop-ServiceOut-Printer
Sends output to a printer.
Use the
-Nameparameter to specify a printer.
Example:
Get-Alias | Out-Printer
Out-Null
Discards output silently.
Useful for suppressing unwanted output.
Example:
Get-Alias | Out-Null5. Formatting Cmdlets
PowerShell includes several cmdlets to format output:
Format-List (fl)
Displays objects in a vertical list.
Shows all properties by default.
Example:
Get-Item HKLM:\SOFTWARE | Format-List *
Format-Table (ft)
Displays objects in a table.
Use the
-AutoSizeparameter to adjust column widths.
Example:
Get-Service | Format-Table Name, DisplayName, Status -AutoSize
Format-Wide (fw)
Displays a single property in multiple columns.
Use the
-Columnparameter to specify the number of columns.
Example:
Get-ChildItem $env:WINDIR | Format-Wide Name -Column 3
Format-Custom (fc)
Displays objects in a custom format.
Rarely used; mainly for advanced scenarios.
Example:
Get-ChildItem $env:WINDIR | Format-Custom * -Depth 2
Last updated