How to remove picture from spreadsheet using PowerShell

Viewed 32

I have a few thousand spreadsheets with a logo floating on top.

I am using Microsoft Excel

I need to remove those images. I was wondering if it was possible with PowerShell.

I'm not allowed to install 3rd party apps

So far I have tried

$Excel = New-Object -comObject Excel.Application
$Excel.Visible = $true

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($Excel)
$wshell.SendKeys('{F5}')  

and so on

but that doesn't work

if I can send following keys while my excel is active

  1. f5
  2. Alt+s
  3. Alt+b
  4. Enter --selects all pictures
  5. Delete --deletes them

then

$Excel.Save()

Any help is appreciated

1 Answers

I was able to solve this

Hopefully, this will help others

Variables

$FilePath = 'E:\something\myfile.xlsx'
$FilePath = Get-Item -Path $FilePath

$Excel = NewObject -comObject Excel.Application
$Excel.Visible = $true

$Excel.DisplayAlerts = $false

Open WorkBook

$Workbook = $Excel.Workbooks.Open($FilePath.fullname,[TYPE]::Missing,$true)

now the main part

$Worksheet = $workbook.Sheets.Item('data')
$Worksheet.Pictures().delete()

$workbook.save()
$workbook.close()
$excel.Quit()
Related