Powershell Export to CSV from Excel Certain Range

Hi, i have the below code, that works.  Require to only pull out a certain range from the excel Document, IE B2:B20

Any one any ideas ?

$sExcelFile="\\pahitfs\it\Sys Admin Team\WindowsServerPatching\2016\WindowsPatching_Reference_2016.xlsx"
$sCSVFile="C:\Patching\Schedule\$wksname.csv"
#$SheetName = "WinA-Test_Thu"

# Get COM Object
$oExcel = New-Object -ComObject "Excel.Application"
# Hide overwrite warning
$oExcel.DisplayAlerts = $false 

# Should Excel be visible?
$oExcel.Visible = $false
# and open excel file
$oExcelDoc = $oExcel.Workbooks.Open($sExcelFile)

# Open Worksheet
#$oWorkSheet = $oExcelDoc.sheets.item($SheetName)
for ($counter = 7; $counter -le $oExcelDoc.Worksheets.count; $counter++) {
    $oWorksheet = $oExcelDoc.Worksheets.item($counter)
    #Get Sheet Name
    $wksname = $oworksheet.name
    # Activate, show it
    $oWorksheet.Activate()
    $sCSVFile="C:\Patching\Schedule\$wksname.csv"
    $oWorksheet.SaveAs($sCSVFile,[Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSVWindows)
}
$oExcelDoc.Close($false)
Start-Sleep 1
# Cleanup COM
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($oWorksheet)|out-null
$oWorksheet=$null
Start-Sleep 1
# Cleanup COM
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($oExcelDoc)|out-null
$oExcelDoc=$null
# Close Excel
$oExcel.Quit()
Start-Sleep 1
# Cleanup COM
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($oExcel)|out-null
$oExcel=$null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()

Open in new window

LVL 7
dann47Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Since Excel does not allow to save a range, there are two possible approaches:
1. copy the range to a new sheet, and export that
2. use PowerShell to read the range and export it.
0
dann47Author Commented:
Which ever would be quicker
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Is this a single column (B) only?
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

dann47Author Commented:
Yeah would be after the range for B
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Replace line 24 with
    $oWorksheet.Range("B2:B20") | select Value2 | export-csv -NoType $sCSVFile

Open in new window

0
Rainer JeschorCommented:
Hi,
is it the same range on all sheets?

Afaik there is no direkt method to export/save a range - you always have to copy the range to a new sheet and exporting that one.

Thanks.
Rainer
0
Rainer JeschorCommented:
@Qlemo: sorry for double posting, had the original question still in cache
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
"Immer schön F5 drücken vor dem Antworten" :D
0
dann47Author Commented:
Many thanks this works, although it is giving a header name of value2
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Do you need it different? This will create a column 'Your Column Name here' instead:
    $oWorksheet.Range("B2:B20") | select @{n='Your Column Name here'; e={$_.Value2}} | export-csv -NoType $sCSVFile

Open in new window

0
dann47Author Commented:
The data already contains the name on line1, so taking that name, so first line it takes is server name, at the moment it outputs
value2
server name
1
2
3

Would like

Server Name
1
2
3

Etc
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You could exclude that row from the range, by using "B3:B20", and then name the column in the script as shown.
Or we just dump the results as text instead of csv:
    $oWorksheet.Range("B2:B20") | select -Expand value2 | Out-File $sCSVFile

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.