# PSWriteExcel – PowerShell Module

This PowerShell Module allows you to create Microsoft Excel (.xlsx) document without Microsoft Excel installed. That basically means you can…

pages

This PowerShell Module allows you to create Microsoft Excel (.xlsx) document without Microsoft Excel installed. That basically means you can…

[View images (3)](#pf-content-image-1-0)

This **PowerShell Module** allows you to create **Microsoft Excel (.xlsx)** document without **Microsoft Excel** installed. That basically means you can create Excel documents on servers, or shared workstations without **Microsoft Office**. It also means it can be run in background and doesn't require any COM objects. What is great about it, it runs on **Windows**, **Linux** and **Mac OS**.

## 💡 Note worthy features

- Allows Microsoft Excel documents to be created in PowerShell
- Doesn't require Microsoft Excel to be installed
- Fairly easy to use
- No license needed. Free to use.
- Open source

### 💡 Useful links

- Code is published on [GitHub](https://github.com/EvotecIT/PSWriteExcel)
- Issues should be reported on [GitHub](https://github.com/EvotecIT/PSWriteExcel/issues)
- Code is published as a module on [PowerShellGallery](https://www.powershellgallery.com/packages/PSWriteExcel)

**PSWriteExcel **is a very basic (at the moment) PowerShell module allowing to create Excel workbooks from PowerShell. It's utilizes an open source module** EPPlus **that's available on [GitHub](https://github.com/JanKallman/EPPlus) so if you're into C# / .NET you can use it as well. This **PowerShell **module was written for **[PSWinDocumentation](https://evotec.xyz/hub/scripts/pswindocumentation-powershell-module/) **and as such is able to take any **Object **and put it into Excel. There are 2 ways to build Excel:

- Where you create new Excel, create WorkSheet and add data to it – **`New-ExcelDocument`**, **`Add-ExcelWorkSheet`**, **`Add-ExcelWorksheetData`** and finally **`Save-ExcelDocument`**

[!\[Visual Studio Code running a PSWriteExcel example script that creates worksheets and writes workbook data while showing worksheet warnings in the terminal.\](/wp-content/uploads/2018/08/img_5b7b2ab585d52.png)](/wp-content/uploads/2018/08/img_5b7b2ab585d52.png)

```powershell
Import-Module PSWriteExcel -Force

$FilePath = "$Env:USERPROFILE\Desktop\PSWriteExcel-Example-Test.xlsx"

$Excel = New-ExcelDocument -Verbose

$ExcelWorkSheet = Add-ExcelWorkSheet -ExcelDocument $Excel -WorksheetName 'Test 10' -Supress $False -Option 'Replace'
$ExcelWorkSheet2 = Add-ExcelWorkSheet -ExcelDocument $Excel -WorksheetName 'Test 2' -Supress $False -Option 'Replace'
$ExcelWorkSheet3 = Add-ExcelWorkSheet -ExcelDocument $Excel -WorksheetName 'This is very long title - Will be cut off at some point' -Supress $false -Option 'Replace'
Add-ExcelWorkSheet -ExcelDocument $Excel -WorksheetName 'Test 1' -Option 'Replace' -Supress $True
Add-ExcelWorkSheet -ExcelDocument $Excel -WorksheetName 'Test 2' -Option 'Skip' -Supress $True
Add-ExcelWorkSheet -ExcelDocument $Excel -WorksheetName 'Test 1' -Option 'Replace' -Supress $True
#Add-ExcelWorkSheetCell -ExcelWorksheet $ExcelWorkSheet -CellRow 1 -CellColumn 1 -CellValue 'Test'

$myitems0 = @(
    [pscustomobject]@{name = "Joe"; age = 32; info = "Cat lover"},
    [pscustomobject]@{name = "Sue"; age = 29; info = "Dog lover"},
    [pscustomobject]@{name = "Jason another one"; age = 42; info = "Food lover"
    }
)

Add-ExcelWorksheetData -ExcelWorksheet $ExcelWorkSheet -DataTable $myitems0 -AutoFit -AutoFilter
Add-ExcelWorksheetData -ExcelWorksheet $ExcelWorkSheet2 -DataTable $myitems0 -AutoFit -AutoFilter
Add-ExcelWorksheetData -ExcelWorksheet $ExcelWorkSheet3 -DataTable $myitems0 -AutoFit -AutoFilter
Add-ExcelWorksheetData -DataTable $myitems0 -Verbose -AutoFit -AutoFilter
Add-ExcelWorksheetData -DataTable $myitems0 -AutoFit -AutoFilter -ExcelDocument $Excel
Save-ExcelDocument -ExcelDocument $Excel -FilePath $FilePath -OpenWorkBook
```

[!\[PSWriteExcel\](/wp-content/uploads/2018/08/PSWriteExcel.gif)](/wp-content/uploads/2018/08/PSWriteExcel.gif)

- Simplified version where you simply pipe an object to **ConvertTo-Excel** command – **$Object | ConvertTo-Excel -Path ‘Export.xlsx' -WorkSheetName ‘MyName'**

```powershell
Import-Module PSWriteExcel -Force

$FilePath = "$Env:USERPROFILE\Desktop\PSWriteExcel-Example-Test1.xlsx"

$myitems0 = @(
    [pscustomobject]@{name = "Joe"; age = 32; info = "Cat lover"},
    [pscustomobject]@{name = "Sue"; age = 29; info = "Dog lover"},
    [pscustomobject]@{name = "Jason another one"; age = 42; info = "Food lover"
    }
)

# Standard way
ConvertTo-Excel -DataTable $myitems0 -FilePath $FilePath -ExcelWorkSheetName 'This is my test' -AutoFilter -AutoFit -FreezeTopRow #-Verbose
# pipeline
$myitems0 | ConvertTo-Excel -FilePath $FilePath -ExcelWorkSheetName 'This is my test2' -AutoFilter -AutoFit -Option Skip -FreezeTopRow #-OpenWorkBook
```

### 💡 Required prerequisites

Before you are able to use this script you need to do few manual steps. Since this script is published as module… it's quite easy to set this up. Just execute command below (accept warnings) .. and start creating **Microsoft Excel **document in **PowerShell**.

```powershell
Install-Module PSWriteExcel

#Update-Module PSWriteExcel
```

You can of course install everything manually from **GitHub** (as everything is published there) but it will be far easier to just use **Install-Module**.

### 💡 Required prerequisites – for linux

On Linux (in my case Ubuntu) the steps are very similar. First you need to get PowerShell up and running on your instance thou. Microsoft has you covered on this **[Installing PowerShell Core on Linux](https://docs.microsoft.com/en-US/powershell/scripting/setup/installing-powershell-core-on-linux?view=powershell-6)** article.

```powershell
# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb

# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb

# Update the list of products
sudo apt-get update

# Install PowerShell
sudo apt-get install -y powershell

# Start PowerShell
pwsh
```

And then simply similar steps as above

```powershell
Install-Module PSWriteExcel -Scope CurrentUser

#Update-Module PSWriteExcel
```

And then just one command… and woila!

```powershell
Get-Process | ConvertTo-Excel -Path 'ThisIsMyExcel.xlsx' -WorkSheetName 'AndWorksheet' -AutoFilter
```

[!\[Linux PowerShell session installing PSWriteExcel and exporting Get-Process results to an Excel workbook named ThisIsMyExcel.xlsx.\](/wp-content/uploads/2018/09/img_5ba1479cd8a33.png)](/wp-content/uploads/2018/09/img_5ba1479cd8a33.png)

[YouTube video](https://www.youtube.com/embed/tMbuOWDEXTE)

### 💡 Quick fixes / helpful tips

- [PowerShellGallery – You are installing modules from an untrusted repository](https://evotec.xyz/powershellgallery-you-are-installing-modules-from-an-untrusted-repository/)
- [Install-Module: The term Install-Module is not recognized as the name of cmdlet, function, script file or operable program](https://evotec.xyz/install-module-the-term-install-module-is-not-recognized-as-the-name-of-cmdlet-function-script-file-or-operable-program/)
- [PSWriteExcel – AutoFit error on Ubuntu – PowerShell Core – The type initializer for Gdip threw an exception.](https://evotec.xyz/powershell-core-the-type-initializer-for-gdip-threw-an-exception/)

<!-- PowerForge markdown source sha256:AB8EBD4349A003D7F55F03FE139462CDB976C9821FA290B2EA02A4D923696B7A; title=True -->

<!-- PowerForge markdown artifact sha256:DCDDCFD2D1326E24A6ADD13523FE821216906D90F5388FB98971646F085851D5 -->
