0

Please bear with me as I am a complete beginner. I am trying to create a phone list where in powershell it pulls data from xlsx. I have an example below, i have also tried Importxlsx but it keeps spitting an error.

The xlsx only has 2 columns Phone List and Department that i want to pull from. Basically Pull "x department" and spit "x phone number"

just need a simple effective command for this.

Function Get-PhoneNumber{
  PARAM($Department)
  $Array = ImportExcel 'C:\Outputs\Internal Phone List 01.01.213 test'
  $Array.Where({_.Department -eq $Department}) | Select-Object -Expandproperty 
  PhoneNumber
}

get-phonenumber -department "ICU"
1
  • Theres a Module for this Called Import excel With thousands of examples you Can re-use Commented Jan 24, 2021 at 19:24

1 Answer 1

1

You can use ImportExcel module.

Since i do not have your XLSX file I need to create one to visualise the example:

# using temp directory to create file
    Set-Location $env:TEMP
    $excelFiles = "ImportExcelHowTo.xlsx"
# remove any pre-existing file
    Remove-Item $excelFiles -ErrorAction SilentlyContinue
    
    # create a file with some data
    $data = ConvertFrom-Csv -InputObject @"
    Phone,Dept
    111-111-1111,James Mary
    222-222-2222,Patricia John
    333-333-3333,Jennifer Robert
    444-444-5555,Michael Linda
    555-555-5555,Elizabeth William
    "@

# export data to XLSX file
    $data | Export-Excel $excelFiles

# open the file if you need
# Invoke-Item $excelFiles

Now I can read the Excel file using Import-Excel function

$data2 =   Import-Excel -Path $excelFiles -WorksheetName Sheet1

It will look like that:

Phone        Dept             
-----        ----             
111-111-1111 James Mary       
222-222-2222 Patricia John    
333-333-3333 Jennifer Robert  
444-444-5555 Michael Linda    
555-555-5555 Elizabeth William

More examples on my blog or ImportExcel GitHub repo.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.