r/PowerShell • • Dec 23 '19

Blog Post: PowerShell and Excel: Yes, They Work Together

Hey guys, I just wrote a shiny new blog post you may enjoy.

Summary: Marry PowerShell and Excel together by exporting and importing Excel workbooks, inspecting cells and more in this tutorial!

https://adamtheautomator.com/powershell-excel-tutorial/

124 Upvotes

30 comments sorted by

13

u/northendtrooper Dec 23 '19

Good stuff. Unfortunately some of the networks we work on prevent installing 3rd party modules. Would be nice to see how to work with excel within powershell as native with the com object.

4

u/logicalmike Dec 23 '19

In that case, I would attempt to explain that the complete installation of Excel not only consumes a license but has a ton more attack vectors than the importexcel module. Personally, i wouldn't install either on a production server, but I'd much rather install a small open source module vs a major office product.

3

u/adbertram Dec 23 '19

I agree. Worst case scenario is to install ImportExcel on a "insecure" computer, do its thing and then move the file back out to the "secure" area.

10

u/IsThatAll Dec 23 '19

Would be nice to see how to work with excel within powershell as native with the com object.

Its very ugly and time consuming, eg.

https://lazywinadmin.com/2014/03/powershell-read-excel-file-using-com.html

11

u/whoisearth Dec 23 '19

Its like shovelling insanity on top of shit lol.

5

u/motsanciens Dec 24 '19

There's nothing crazy going on, here--what's the big fuss?

1

u/orwiad10 Dec 23 '19

Does importing a dll count as a 3rd party module?

3

u/da_chicken Dec 24 '19

Such policies invariably prohibit installation of third party software of any form that IT hasn't approved. No, you can't circumvent the policy by weaseling.

-1

u/orwiad10 Dec 24 '19

You sound very smart.

1

u/weiyentan Dec 23 '19

What constitutes installing a module? Would copying files constitute installing a module? Can you ‘copy’ to temp directory? Or a personal directory? That’s all it takes

1

u/MMEnter Dec 27 '19

I am not sure about OP's environment, but I have copied while modules into my project and called the functions from within.

1

u/weiyentan Dec 29 '19

That’s my point. You don't have to install powershell modules, you can just copy the module to a path. Then Import the module from that path.

1

u/uslackr Dec 24 '19

Yeah, you should write that article. I'm using ImportExcel. Its excellent

4

u/Swarfega Dec 24 '19

Converting CSV Files to Excel with PowerShell

The title says this but all examples are converting Excel to CSV. The title just needs correcting.

1

u/shallanlol Dec 24 '19

It's a pity word integration sucks

1

u/adbertram Dec 24 '19

It’s doable but ugly.

1

u/shallanlol Dec 24 '19

My bumper sticker

1

u/adbertram Dec 24 '19

Ba dum tiss

1

u/rantingdemon Dec 24 '19

What would be the use cases for integration with Word?

3

u/beerchugger709 Dec 24 '19

https://github.com/paulwetter/DocumentConfigMgrCB/

Here's a script that documents (in incredible detail) an entire SCCM environment. Among the outputs, is a docx without any need for word to be installed. Lol, the dude even added switches to customize the cover page with logos and stuff

2

u/MrWinks Dec 24 '19

Search and replace text in a template file, then save the file. Easy to make sheets with names and such on them this way.

2

u/ipreferanothername Dec 24 '19

https://github.com/EvotecIT/PSWriteWord

this guy posts around here regularly, i havent had a need to use the module myself but maybe give it a whirl. he has lots of other modules. https://github.com/EvotecIT

1

u/MrWinks Dec 24 '19

I use the com objects directly and got comfortable with the process, but thank you!

2

u/shallanlol Dec 27 '19

Problem is resources and runtime of the word objects vs Excel for similar operations

1

u/shallanlol Dec 27 '19

I work in education. Generally reports in docx are used by teachers in common and other behavioural templates.

There are much better methods of doing it so it's not a solid use case.

However the way the interaction works with word against powershell makes it much more annoying and resource consuming.

3

u/BigR0n75 Dec 23 '19

This and DbaTools are my favorite, most commonly used modules. I've barely scratched the surface with either and I can already see how cool things they can do together.

2

u/darty1713 Dec 23 '19

Fantastic, thanks, it's great. I'm just starting to learn powershell and this type of post is perfect.

1

u/dittbub Dec 24 '19

I've replaced SQL csv with powershell excel files at work. users love it cause they can actually open it as an attachment, and commas won't fuck the whole thing up!

-5

u/jsiii2010 Dec 23 '19

Or export to csv then import in excel. Excel likes ascii better than unicode.

1

u/volvo64 Dec 24 '19

Not to mention the csv support in Powershell is 10x faster. If I’m working with any large amount of data I start with a CSV then push it to Excel and manipulate from there. Much faster.