param ( [parameter( Mandatory = $False, ValueFromPipeline = $True )] [string] $excelfile, [switch] $h, [parameter( ValueFromRemainingArguments = $true )] [object] $invalidArgs ) if ( $h -or $invalidArgs -or $excelfile.Contains( "?" ) -or ![System.IO.File]::Exists( $excelfile ) ) { if ( ![string]::IsNullOrWhiteSpace( $excelfile ) -and !$excelfile.Contains( "?" ) -and ![System.IO.File]::Exists( $excelfile ) ) { Write-Host Write-Host "ERROR: " -ForegroundColor Red -NoNewline Write-Host ( "Excel file `"{0}`" not found" -f $excelfile ) } elseif ( $h -or [bool] $invalidArgs ) { Write-Host Write-Host "ERROR: " -ForegroundColor Red -NoNewline Write-Host "Too many argument(s)" } Write-Host Write-Host "ReadExcel.ps1, Version 1.00" Write-Host "Read an Excel file and show all cells for all sheets" Write-Host Write-Host "Usage: " -NoNewline Write-Host "./ReadExcel.ps1 excelfile" -ForegroundColor White Write-Host Write-Host "Where: " -NoNewline Write-Host "excelfile " -ForegroundColor White -NoNewline Write-Host "is the Excel file to be read" Write-Host Write-Host "Notes: This script requires Excel." Write-Host " This script " -NoNewline Write-Host "may " -ForegroundColor White -NoNewline Write-Host "sometimes leave Excel open after running." Write-Host " Return code (`"errorlevel`") 1 in case of errors, otherwise 0." Write-Host Write-Host "Credits: Based on article by François-Xavier Cat" Write-Host " https://lazywinadmin.com/2014/03" -ForegroundColor Darkgray Write-Host " /powershell-read-excel-file-using-com.html" -ForegroundColor Darkgray Write-Host Write-Host "Written by Rob van der Woude" Write-Host "https://www.robvanderwoude.com" Exit 1 } $objExcel = New-Object -ComObject Excel.Application $workbook = $objExcel.Workbooks.Open( $excelfile ) $workbook.Sheets | ForEach-Object { if ( ![string]::IsNullOrEmpty( $_.Range( "A1" ).Text ) ) { Write-Host $_.Name # sheet name Write-Host ( "=" * $_.Name.Length ) # underline sheet name for ( $row = 1; $row -le $_.UsedRange.Rows.Count; $row++ ) { for ( $column = 1; $column -le $_.UsedRange.Columns.Count ; $column++ ) { Write-Host ( "({0}{1})`t{2}" -f ( [char] ( $column + 64 ) ), $row, $_.Columns.Item( $column ).Rows.Item( $row ).Text ) } Write-Host } } } [void] $workbook.Close( ) [void] $objExcel.Quit( )