Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is is possible to launch excel in readonly mode from Powershell

I use Invoke-Item "some-file.xlsx" to launch Excel and open a file. Now I would like to open this file in read only mode. It this possible from PowerShell?

To clearify, I am not looking to open the Excel sheet and read data from it to the PowerShell script, the script is intended to help the user finding a file and opening it.

like image 395
Jan H Avatar asked Nov 25 '25 22:11

Jan H


1 Answers

Invoke-item only allows the default action for a file.
To open the file as read only, either:

  • Set read-only attribute on file, launch file, unset read-only attribute,

    attrib +r "some-file.xlsx"
    Invoke-Item "some-file.xlsx"
    attrib -r "some-file.xlsx"
    

or

  • Invoke excel directly, and pass /r as the command file

    $xl = (Get-ItemProperty -Path HKCR:\Applications\EXCEL.EXE\Shell\OpenAsReadOnly\command).'(default)'
    $fle = '"some-file.xlsx"'
    & $xl $fle
    

check Executing a Command stored in a Variable from Powershell to get a syntax that works for you

like image 80
SeanC Avatar answered Nov 28 '25 17:11

SeanC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!