如何使用PowerShell在Excel表格中使用数据?

我想做一些棘手的事情来使用PowerShell中的Excel数据。 我在Excel表中有以下表格。

在这里输入图像说明

我想使用PowerShell从这个表中读取数据并做一个简单的计算。 例如, if ($Reader = Seafreight) {$Result = $TransportE * $CO2footprint}但我不知道如何将excel数据作为variables导入到PowerShell中。 有任何想法吗?

这是我将如何做到这一点:

首先把下面的函数放到你的Powershellconfiguration文件中,这会给你一个Get-Clipboard cmdlet,它总是可用的。

 function Get-Clipboard([switch] $Lines) { if($Lines) { $cmd = { Add-Type -Assembly PresentationCore [Windows.Clipboard]::GetText() -replace "`r", '' -split "`n" } } else { $cmd = { Add-Type -Assembly PresentationCore [Windows.Clipboard]::GetText() } } if([threading.thread]::CurrentThread.GetApartmentState() -eq 'MTA') { & powershell -Sta -Command $cmd } else { & $cmd } } 

然后,您只需从Excel复制表格(确保每个列都有一个标题),然后在Powershell控制台中input以下内容:

 $data = Get-Clipboard | ConvertFrom-Csv -Delimiter "`t" $data