0

I want to do some tricky things to use excel data in PowerShell. I have the following table in the excel sheet.

enter image description here

I want to use PowerShell to read the data from this table and do a simple calculation. For example, if ($Reader = Seafreight) {$Result = $TransportE * $CO2footprint} But I do not know how to import the excel data into PowerShell as variables. Any ideas?

1
  • 1
    There are solutions for this depending on your needs. Here is a custom cmdlet for importing for example. You could also just export to CSV from excel and import using Import-csv which is simpler. Depends on what your end game is. Commented Aug 26, 2015 at 14:23

1 Answer 1

1

This is how I would do it:

First put the following function into you Powershell profile, this will give you a Get-Clipboard cmdlet which will always be available.

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
    }
}

Then all you need to is copy your table from Excel (make sure you have a header for each column) then type the following into your Powershell console:

$data = Get-Clipboard | ConvertFrom-Csv -Delimiter "`t"
$data
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.