Excel (XLSX) is one of the most popular file formats used as simple databases for storing different structured data. Administrators can use XLS/XLSX files to store and retrieve various types of data in their scripts. This guide explains how to import an Excel file directly into a PowerShell script.
There are two popular ways to access data in an Excel file from PowerShell:
- Using COM objects (requires Microsoft Excel to be installed on the computer)
- Using the third-party Import-Excel module (can be used without installing the Excel app)
Requirements
Excel COM object
- Microsoft Excel installed
- Windows PowerShell or PowerShell 7 (Windows)
ImportExcel module
- ImportExcel PowerShell module
- No Microsoft Excel installation required
Important. Excel COM automation is intended primarily for interactive use on Windows workstations where Microsoft Excel is installed. We don’t recommend or support unattended, server-side Office automation. For scheduled tasks, servers, and other non-interactive environments, you should use the ImportExcel module instead of automating Excel through COM.
PowerShell: Import data from an XLSX file using Excel COM object
For example, I have a simple file, inventory.xlsx, from which I want to extract data and import it into a PowerShell script. The XLSX file contains a list of users in the following format: SamAccountName, Name, Address, Email, Department.

One way to read XLSX files from PowerShell is to automate Microsoft Excel through COM. This method requires Excel to be installed and is primarily suitable for interactive scripts running on Windows workstations:
$Excel = New-Object -ComObject Excel.Application
The Excel process runs in the background by default and is not displayed on the desktop. To make the app window visible, run the command:
$Excel.visible = $true
Now, open the target XLSX file (book):
$WorkBook = $Excel.Workbooks.Open("C:\PS\inventory.xlsx") You should consider wrapping Excel automation code in a try { } finally { } block to ensure that the workbook is closed and the Excel COM object is released even if an error occurs.
Note. If the Excel workbook is password-protected, you must provide the password when opening it with the Excel COM object. For example:
$Workbook = $Excel.Workbooks.Open(
"C:\PS\inventory.xlsx",
$null,
$true,
$null,
"MyPassword"
)The ImportExcel module has limited support for password-protected workbooks. If the workbook is encrypted, you may need to remove the password first/use Excel COM automation instead.
An Excel workbook can contain multiple worksheets. To list all worksheets in a file, run:
$WorkBook.Sheets | fl Name, Index

A specific worksheet can be selected by its name using this command:
$WorkSheet = $WorkBook.Sheets.Item("UserInfo") Now you can get the underlying value of a specific cell in an Excel spreadsheet:
$Worksheet.Range("A2").Value2 You should use Value2 to retrieve the underlying cell value without Excel’s display formatting.

Script that builds PSCustomObject object for each row
But most often you want to read the entire contents of a workbook and import all the data into a PowerShell object. The following script reads cells and builds a PSCustomObject object for each row.
# Get the number of columns
$UsedRange = $Worksheet.UsedRange
# Get the number of rows in a workbook
$RowCount = $UsedRange.Rows.Count
$users = @()
for ($row = 2; $row -le $RowCount; $row++) {
$samAccountName = $Worksheet.Cells.Item($row, 1).Value2
$name = $Worksheet.Cells.Item($row, 2).Value2
$address = $Worksheet.Cells.Item($row, 3).Value2
$email = $Worksheet.Cells.Item($row, 4).Value2
$department = $Worksheet.Cells.Item($row, 5).Value2
$users += [PSCustomObject]@{
SamAccountName = $samAccountName
Name = $name
Address = $address
Email = $email
Department = $department
}
}
Note. Our script uses the Value2 property to retrieve the underlying cell value rather than the formatted value displayed by Excel. This is especially important when you are working with numeric/date values, because the Text property returns the cell’s formatted, potentially locale-dependent representation.
Thus, we imported all the rows from the Excel file into the PSObject. List the resulting object on the screen:
$users

Access the value of individual item in object
You can access the value of an individual item in this object using this syntax.
Display only usernames:
$users.Name
Display the name of the second user in the list:
$users.Name[1]

After importing data from Excel into PowerShell, you can close the workbook and exit the application:
$Workbook.Close($false) $Excel.Quit()
Note. When working with Excel COM objects, calling Quit() is not always sufficient to completely terminate the Excel process. PowerShell may still hold references to COM objects, causing Excel.exe to remain running in the background. In order to avoid orphaned Excel processes, you need to explicitly release the COM objects after closing the workbook:
$Workbook.Close($false)
$Excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Worksheet) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Workbook) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Excel) | Out-Null
Remove-Variable Worksheet, Workbook, Excel
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
Using imported Excel data in a loop
Once the Excel data has been imported into a PowerShell object, you can iterate through each row and perform admin tasks. For example, to display the name of every user, use the following command:
foreach ($user in $users) {
Write-Host $user.Name
} In real-world administration, the imported data is often used to perform bulk operations. For example, you can look up AD accounts listed in the Excel file:
foreach ($user in $users) {
Get-ADUser -Identity $user.SamAccountName
} You can also use the imported values to create users, update attributes, assign group memberships, or perform other automated admin tasks.
More real-world administration examples
You can use the imported Excel data for many common AD and Microsoft 365 admin tasks. For example, update the Department attribute for multiple users:
foreach ($user in $users) {
Set-ADUser `
-Identity $user.SamAccountName `
-Department $user.Department
} In order to add users to an AD security group, run the following command:
foreach ($user in $users) {
Add-ADGroupMember `
-Identity "VPN Users" `
-Members $user.SamAccountName
} You can also use imported data to automate Microsoft 365 admin tasks (such as assigning licenses, updating mailbox settings, or modifying group memberships by combining the imported objects with the appropriate Microsoft Graph/Exchange Online PowerShell cmdlets).
Working with large datasets
When importing large Excel files (tens of thousands of rows), you may want to filter or summarize the data before processing it further. For example, count the number of imported records:
$users | Measure-Object
You can filter the imported data with the command:
$users |
Where-Object Email -like "*.contoso.com"
To export the imported data to a CSV file for further processing, run the command:
$users |
Export-Csv "C:\Reports\Users.csv" -NoTypeInformation
For very large datasets, you should consider processing only the required worksheets and columns to reduce memory usage and improve performance.
Read XLSX Files Using the ImportExcel Module
The PowerShell Gallery offers the ImportExcel module, which can be used to read data from XLS/XLSX files. The main advantage of this module is that it can be used without installing the MS Excel app on the computer. Keep in mind that ImportExcel is a community PowerShell module available from the PowerShell Gallery and is not included with Windows by default. The ImportExcel module is well suited for unattended automation because it does not require Microsoft Excel/Excel COM automation. This makes it a better choice for scheduled tasks, servers, and other non-interactive environments.
Download and install ImportExcel module
To download and install the module, run:
Install-Module ImportExcel -Scope CurrentUser

The same module also includes the Export-Excel cmdlet for writing PowerShell objects back to XLSX files.
Once the installation is complete, restart your PowerShell console and verify that the module has been installed:
Import-Module ImportExcel Get-Module ImportExcel

Note. The ImportExcel module provides much more than just the Import-Excel cmdlet. It also includes Export-Excel for writing PowerShell objects to XLSX files, along with features for creating formatted tables, charts, pivot tables, and conditional formatting without requiring Microsoft Excel.
Import all values from Excel file to PowerShell
Use the following command to quickly import all values from an Excel file to PowerShell:
$myUsers = Import-Excel "C:\PS\inventory.xlsx" $myUsers

Importing data from a worksheet
Data from the first worksheet of the Excel document is imported by default. Show the list of worksheets in the file:
$excel = Open-ExcelPackage -Path "C:\PS\inventory.xlsx" $excel.Workbook.Worksheets|select Name, Index

Import data from a specific worksheet (by its name):
Import-Excel -Path "C:\PS\inventory.xlsx" -WorksheetName UserInfo
Use the StartRow, EndRow, StartColumn, and EndColumn parameters to specify the range of cells in the worksheet you want to import into PowerShell:
Import-Excel -Path "C:\PS\inventory.xlsx" -StartRow 1 -EndRow 4 -StartColumn 2 -EndColumn 3

To clear an Excel object from memory, run the following command:
Close-ExcelPackage $excel
Wrapping Up
If Microsoft Excel is installed, COM automation provides full access to Excel workbook functionality and can be useful for interactive scripts running on an admin’s workstation. However, we don’t recommend/support unattended, server-side Office automation. Keep in mind that Excel COM automation can cause hangs, orphaned EXCEL.EXE processes, and other reliability issues in non-interactive environments.
For scheduled tasks, servers, and other unattended automation, you should avoid Excel COM automation and use the ImportExcel module/another library that can process XLSX files without requiring Microsoft Excel to be installed.
What are the main ways to import Excel (XLSX) data into PowerShell?
There are two common approaches:
- Excel COM automation, which requires Microsoft Excel to be installed and provides full access to workbook features.
- ImportExcel PowerShell module, which doesn’t require Excel and is generally preferred for automation, scheduled tasks, and server environments.
Do I need Microsoft Excel installed to read XLSX files in PowerShell?
Not necessarily. If you use the ImportExcel PowerShell module, you can import XLSX files without installing Microsoft Excel. Excel is only required when using COM automation.
When should I use Excel COM objects instead of the ImportExcel module?
Use Excel COM automation when you need Excel-specific functionality, such as working with password-protected workbooks or advanced workbook features. For unattended scripts and server environments, the ImportExcel module is usually the better choice because it is faster and doesn’t depend on Excel.
Can PowerShell import data from a specific worksheet?
Yes. Both methods allow you to import data from a particular worksheet. With the ImportExcel module, use the -WorksheetName parameter. With COM automation, select the worksheet by name using the workbook’s Sheets collection.
How can I import only part of an Excel worksheet?
The ImportExcel module supports importing specific ranges by using parameters such as -StartRow, -EndRow, -StartColumn, and -EndColumn.
