0

I've got a excel serverlist where column D is hostnames and column F area (like dev, int, prd). Now I want to read this excelfile with powershell and printout each hostname that has the area dev. What do I have to change?

$FilePath = "C:\serverlist.xlsx"
$SheetName = "serverlist"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$WorkBook = $objExcel.Workbooks.Open($FilePath)
$WorkSheet = $WorkBook.sheets.item($SheetName)


$Range = $WorkSheet.Range("F2:F150").Text

Foreach ($cell in $Range) {

    If ($cell -like "dev") {

        Write-Host "hostname from  Column D"

    }

}

1 Answer 1

3

Save your spreadsheet as a CSV and you will be able to process the list much faster. Then you can use following code, change values after -Header to column names relevant to you:

$FilePath = "C:\serverlist.csv"
$serverList = Import-Csv $FilePath 
    -Header "ColumnA","ColumnB","ColumnC","HostName","Area"
$serverList | where {$_.Area -like"dev" } | Select-Object HostName
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.