SEC505
  • Welcome to "Securing Windows with PowerShell: A Deep Dive into SEC505"
    • Sec 505.1
      • Intro to Ps
      • Tips for Executing Commands
      • Getting Help in PowerShell
      • Aliases in PowerShell
      • Objects, Properties, and Methods
      • Get-Member (Alias: gm)
      • Drives and Environment Variables
      • Your Profile Script(s)
      • Functions, Cmdlets, and Modules
      • The PowerShell Gallery
      • Exporting, Importing, an d Converting Pages Reusable content Files Object Data
        • Select-Object (Alias: Select)
          • Arrays Are like In-Memory Database Tables
      • Search Event Logs
      • Hashtables and Splatting
      • Flow Control (All in one)
      • Functions
Powered by GitBook
On this page
  1. Welcome to "Securing Windows with PowerShell: A Deep Dive into SEC505"
  2. Sec 505.1

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.xml

Key Points:

  • XML files preserve complex, nested object structures.

  • Use the -Depth parameter 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-JSON

Key Points:

  • JSON is a lightweight format, ideal for web APIs and modern applications.

  • Use the -Depth parameter to handle nested objects.

  • Use the -Compress parameter 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.html

Key Points:

  • HTML is ideal for creating human-readable reports.

  • Use the -Property parameter 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 -Paging parameter 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 -Force

Out-GridView

  • Displays output in a graphical table.

  • Use the -PassThru parameter to filter and pass selected objects to the next command.

Example:

Get-Service | Out-GridView -PassThru | Stop-Service

Out-Printer

  • Sends output to a printer.

  • Use the -Name parameter to specify a printer.

Example:

Get-Alias | Out-Printer

Out-Null

  • Discards output silently.

  • Useful for suppressing unwanted output.

Example:

Get-Alias | Out-Null

5. 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 -AutoSize parameter 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 -Column parameter 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
PreviousThe PowerShell GalleryNextSelect-Object (Alias: Select)

Last updated 2 months ago

Page cover image