> For the complete documentation index, see [llms.txt](https://mohab-yehia.gitbook.io/sec505/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mohab-yehia.gitbook.io/sec505/welcome-to-securing-windows-with-powershell-a-deep-dive-into-sec505/sec-505.1/exporting-importing-an-d-converting-pages-reusable-content-files-object-data.md).

# 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
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FLvD7odrwIP8IA67scNAM%2Fimage.png?alt=media&amp;token=2a281f80-3d58-4487-8ebb-6f2390bec1dd" alt=""><figcaption></figcaption></figure>

**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
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2Fnb4g4ziv1d0Pxm8oC386%2Fimage.png?alt=media&amp;token=30916c5b-0b47-4821-9ea3-0a0923fbf749" alt=""><figcaption></figcaption></figure>

**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 *
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FsjNn6uRzLGUKdPcupvKM%2Fimage.png?alt=media&amp;token=a3d197fd-22c1-4a2f-b091-e8f6d417154b" alt=""><figcaption></figcaption></figure>

**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
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FIvbTvyNDwW6n7u5fDO5p%2Fimage.png?alt=media&amp;token=27d16767-9ab8-4475-8d2d-9b29777dd7c9" alt=""><figcaption></figcaption></figure>

**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
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2Ff17Od4nXs59rJ5jkclv4%2Fimage.png?alt=media&amp;token=02a8baef-f73e-4235-85b9-b90a582647a3" alt=""><figcaption></figcaption></figure>

**Format-Custom (fc)**

* Displays objects in a custom format.
* Rarely used; mainly for advanced scenarios.

**Example:**

```
Get-ChildItem $env:WINDIR | Format-Custom * -Depth 2
```

<figure><img src="https://2325329360-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxV3NRyPsEjkbBdVUnFZk%2Fuploads%2FRZAX1zULI0GlWLFGN5Jo%2Fimage.png?alt=media&amp;token=c51ce071-dd65-4ae7-ac55-cb3388496ac4" alt=""><figcaption></figcaption></figure>
