Skip to content

Instantly share code, notes, and snippets.

@hirenchauhan2
Created October 20, 2020 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirenchauhan2/9b690b6f01c148de215141bb1d1dcc37 to your computer and use it in GitHub Desktop.
Save hirenchauhan2/9b690b6f01c148de215141bb1d1dcc37 to your computer and use it in GitHub Desktop.
Read excel file from Powershell
#Declare the file path and sheet name
$file = "C:\Excelfile.xlsx"
# name of the sheet you want to read
$sheetName = "Sheet1"
#Create an instance of Excel.Application and Open Excel file
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible = $false
#Count max rows
$rowMax = ($sheet.UsedRange.Rows).count
#Declare the starting positions
$rowName, $colName = 1, 1
$rowName, $colAge = 1, 2
#loop to get values from rows
for ($i = 1; $i -le $rowMax - 1; $i++) {
$name = "COMMUNITY\" + $sheet.Cells.Item($rowName + $i, $colName).text
$age = $sheet.Cells.Item($rowName + $i, $colAge).text
Write-Host ("Name is: " + $name)
Write-Host ("Age is: " + $age)
Write-Host ("-----------------")
}
#close excel file
$objExcel.quit()
Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment