Skip to content
PowerShell wrapper around Python for .NET to invoke Python from PowerShell
PowerShell
Branch: master
Clone or download
Latest commit 90ffff2 Nov 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Fix linux. Nov 14, 2019
.gitignore Add manifest Jan 5, 2018
LICENSE Initial commit Jan 3, 2018
README.md Fixin er up. Nov 14, 2019
snek.jpg Adding snek Jan 3, 2018

README.md

Snek

PowerShell wrapper around Python for .NET to invoke Python from PowerShell

Install snek

Install-Module snek

Requirements

  • Python v2.7, v3.5, v3.6, or v3.7 (defaults to python 3.7)

Functions

  • Use-Python
  • Invoke-Python
  • Import-PythonRuntime
  • Import-PythonModule
  • Install-PythonModule
  • Uninstall-PythonModule
  • Use-PythonScope
  • Set-PythonVariable

Invoke Python Code (v3.7)

Use-Python { 
    Invoke-Python -Code "print('hi!')" 
}
    
hi!

Invoke Python Code (v2.7)

PS > Use-Python { 
    Invoke-Python -Code "print('hi!')" 
} -Version v2.7
    
hi!

Returning A Value from Python to PowerShell

Due to the use of dynamic the type must be cast to the expected type so you need to specify the -ReturnType parameter to do so.

Use-Python {
    Invoke-Python "'Hello'" -ReturnType ([String])
}

Imports the numpy Python module and does some math

Access methods of modules directly!

Use-Python {
    $np = Import-PythonModule "numpy"
    [float]$np.cos($np.pi * 2)

    [float]$np.sin(5)
    [float]($np.cos(5) + $np.sin(5))
} -Version v3.7

Output

1
-0.9589243
-0.6752621

Manage pip

Format is Install-PythonModule <package>

Install-PythonModule requests

Or similarly:

Uninstall-PythonModule requests

Using Scopes

You can use Python scopes to string together multiple Invoke-Python calls or to pass in variables from PowerShell.

Use-Python {
    Use-PythonScope {
        Invoke-Python -Code "import sys" 
        Invoke-Python -Code "sys.version" -ReturnType ([string]) 
    }
}

Passing a .NET Object to Python

class Person {
    [string]$FirstName
    [string]$LastName
}

Use-Python {
    Use-PythonScope {
        $Person = [Person]::new()
        $Person.FirstName = "Adam"
        $Person.LastName = "Driscoll"
        Set-PythonVariable -Name "person" -Value $Person

        Invoke-Python -Code "person.FirstName + ' ' + person.LastName" -ReturnType ([string])
    }
}
You can’t perform that action at this time.