Get the App
SLTechnology News&Howtos  ›  Servers  › 

Powershell Excel Module

Shulou Source: shulou.com Published: 2022-06-03 03:43:49 10月24日 Update

Reference documentation:

Https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/25/introducing-the-powershell-excel-module-2/

Https://github.com/dfinke/ImportExcel

It always seems to be a problem to export data from Powershell and easily process it, for example, to export to CSV format.

Popularize a knowledge of CVS Comma-Seperated Value files. Oh, I just knew it, too, and I wondered before.

For example, what we used to do is:

Get-Process | Export-Csv c:\ Temp\ ps.csv # generate CSV file

Invoke-Item c:\ temp\ ps.csv # Open it

There is another way, provided that the computer has to install the EXCEL program, using the EXCEL COM component to open it. As follows:

$xl = New-Object-ComObject excel.application # generate an instance of the EXCEL class

$xl.visible = $true # Let this instance be displayed

$xl.workbooks.add () # add a default table

The good news is that now there is an EXCEL module to support, if your computer's Powershell version is 5.0, you can directly install this module with the following command, and then you can directly call it, a very powerful function.

PS C:\ > Install-Module importexcel

Untrusted repository

You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy

Value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?

[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): y

After the installation was successful, I was pleasantly surprised to find:

Get-Process | Export-Excel c:\ temp\ psnew.xlsx-show

Of course, this is just the beginning, and more powerful features are yet to come. Open an ISE, put the following code in, and run it to see the result:

Rm $file-ErrorAction Ignoreps | where company | select Company,PagedMemorySize,PeakPagedMemorySize | Export-Excel $file-Show-AutoSize `- IncludePivotTable`-IncludePivotChart `- ChartType ColumnClustered`-PivotRows Company `- PivotData @ {PagedMemorySize='sum';PeakPagedMemorySize='sum'}

Of course, there are some more incredible implementation effects:

$ps = ps$ps | Export-Excel.\ testExport1.xlsx-WorkSheetname memory `- IncludePivotTable-PivotRows Company-PivotData PM`-IncludePivotChart-ChartType PieExploded3D$ps | Export-Excel.\ testExport1.xlsx-WorkSheetname handles `- IncludePivotTable-PivotRows Company-PivotData Handles`-IncludePivotChart-ChartType PieExploded3D-Show

Tags: That is powerful functional instance file module computer generation surprise success incredible code example practice premise just command good news effect data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno macOS Xiaomi MariaDB MySQL