Import Data from Powershell object into Excel Spreadhseet - excel

I have built a powershell object in a script. In this same script I need to take that object (or parts of it) and load them into an Excel spreadsheet. The excel spreadsheet is already saved and has headers. I am able to find my .xlsx file, open it, save and close but not sure how to actually get my object into the file. This script will be run a lot so ultimately I need the data to just append to the first available/empty row.
Here is the script that is opening the excel file:
$excel_file_path = 'C:\Users\ME\Documents\Test\SFTPTest.xlsx'
# Instantiate the COM Object
$Excel = New-Object -ComObject Excel.Application
$Excel.Application.Visible = $true
$Excel.DisplayAlerts = $false
$ExcelWorkBook = $Excel.Workbooks.Open($excel_file_path)
$ExcelWorkSheet = $Excel.Worksheets.item("SFTPTransfers")
$ExcelWorksheet.activate() | Out-Null
The headers in my excel spreadsheet are on row 1 and there are 9 columns (A-I)
My PS Object is a large oject converted from a JSON object. I only need 9 properties of this object to go into this spreadsheet. For example, I have a property called Object1.ServerName that needs to go into the my excel file under ServerName column...etc.

Related

Powershell export csv results to multiple sheets in an excel workbook

I run a lot of scans against our AD looking for deficiencies (i.e. users without an email, user ID blank, on-leave but AD account not disabled, etc) and all my results are exported to csv files. I'd like each csv result stored on a sheet in a single excel workbook. I've found a lot of sites showing how to convert a csv to an xls or how to export to a single xls sheet but I can't find anything else meeting my needs. I don't have the ability to import the Import-Excel / Export-Excel modules from the PS gallery. Any suggestions on how I can export 3 CSV files (actually array objects - not sure they need to be exported to CSV first) to three sheets in a workbook?
What I've found so far:
# Create Excel object
$excel = new-object -comobject Excel.Application
# Create a new workbook
$workbook = $excel.workbooks.add()
# Name a worksheet
$workbook.WorkSheets.Item(1).Name = "Users"
# Add data from a csv to the current sheet
$workbook = $excel.workbooks.add(“C:\export_users.csv”)
What I can't figure out is how to add data from additional csv files onto additional sheets in the workbook.
Add another worksheet with another Add().
$excel = New-Object -ComObject Excel.Application
$wb1 = $excel.workbooks.add()
$wb1.WorkSheets.Item(1).Name = 'Users 1'
$wb1.Worksheets.Item(1).Name
$wb1.Worksheets.Count
$wb1.Worksheets.Add() | Out-Null
$wb1.WorkSheets.Item(2).Name = 'Users 2'
$wb1.Worksheets.Item(2).Name
$wb1.Worksheets.Count
I have had some success using the ImportExcel module for PowerShell.

Workbook.SaveAs method is popping Save As dialog box

This was working fine until this weekend, now it's stopped..
I have a powershell script that opens an Excel file as read only, copies a sheet from that file into a new file, then saves the new file to a different location. The script still does all of that, gets to the very end, saves the file to the new location, but now it is opening a "Save as" dialog box, which prevents the script from ever completing. Something change with Powershell? Or the SaveAs method?
# Specify file names/paths
$sourceFile = "C:\source.xlsx"
$exportFile = "C:\export.xlsx"
# Open Excel, hidden/suppress alerts
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$Excel.DisplayAlerts = $false
# Open the source workbook in read-only mode
$sourceWorkbook = $Excel.Workbooks.Open($sourceFile,$null,$true)
# Create a new/blank workbook
$newWorkbook = $Excel.Workbooks.Add()
# Copy sheet from source workbook to new workbook
$sourceWorkbook.Sheets("SheetName").Copy($newWorkbook.Sheets[1])
# Save the new workbook
$newWorkbook.SaveAs($exportFile)

Paste\Return into Cell A1 of open Excel sheet

I wish to take a string generated in PowerShell and paste\return that string into Cell A:1 of an OPEN spreadsheet. I have researched this but I dont understand the excel specific syntax.
Do I need to identify and declare the PID for the specific Excel workbook thats running and if so how?
I know how to create a new workbook and add the text to cell A:1 but not one which is already open. It must also "press return" to execute the spreadsheet functions once pasted.
I dont know where to start other than: ((new-object -com "excel.application").Workbooks.Add()).application.Visible=$True
I have scoured the web but have not found any examples that make sense. Please help
You can achieve it by using the below script
#Specify the path of the excel file
$FilePath = "path\test.xlsx"
#Specify the Sheet name
$SheetName = "Sheet1"
# Create an Object Excel.Application using Com interface
$objExcel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName)
$WorkSheet.Cells.Item(1,1) = "Link" #Updates the first cell (A1)
$WorkBook.Save()
$WorkBook.Close()
Make sure the file has write permissions from PowerShell scripts.
If you want to edit the sheet which is already open, change the path of the file and mention the sheet name properly and it would work as expected.

How to convert an executable excel file to binary (exe to xlsb) with PowerShell?

I'm trying to get data from an executable excel file (which is an excel file compressed and wrapped into an .exe) without success using PowerShell. I can open it, but I can't get the values stored in it.
I've been able to get data from binary excel files though (.xlsb).
$FilePath = "d:\database\temp.xlsb"
$SheetName = "NDC"
$objExcel = New-Object -ComObject Excel.Application
$WorkBook = $objExcel.Workbooks.Open($FilePath)
$WorkSheet = $Workbook.sheets.item($SheetName)
$value = $Worksheet.Range("A1").Text
Then, my question is, is it possible to open the excel exe file and save the file with a format that can be accessed with PowerShell?

When embedding an OLEObject (Word/XLS/PDF doc) into Excel via powershell, unable to save that

When I embedding an OLEObject (Word/XLS/PDF doc) into Excel via powershell, I'm unable to save the excel file (I'd like to save manually, not via PShell - but does not work automatically either). If I insert object like *.txt, png,jpeg - there is no problem with saving the target excel file (both auto and manually saving works).
Any idea? Thanks.
The relevant part of the code is:
$missing=[System.Type]::missing
$excel = New-Object -ComObject Excel.Application
$excel.visible=$true
$wrkbook = $excel.Workbooks.Add()
$wrksheet = $wrkbook.Worksheets.Item(1)
$file = ("link")
$icon =$wrksheet.OLEObjects().Add($missing,$file,$false,$false,$missing,$missing,$missing,$missing,$missing,$missing,$missing)

Resources