Endpoint Insights

How to Query a Tesla with PowerShell

Topics: Endpoint Insights

How to Query a Tesla with PowerShell

As some of you might know, I needed to replace my car, so I replaced it with a Tesla Model 3 (base model). There is a long story about why I wanted to see what information I could query from my Tesla that I’m not going to get into. What I will tell you is that while researching this topic, I found out that I could query a Tesla with PowerShell! It is kind of cool what you can do with a Tesla via the API.

In this post, I am giving you my script, so that you can query your Tesla with PowerShell too. Below are the steps you need to take.

Tesla API

As far as I know, there is no official API listing for Tesla. The unofficial API documentation, however, can be found on https://tesla-api.timdorr.com/. Based on the unofficial API doc, you can determine things like the car’s model and color, and even apply software updates to the car remotely!

Tesla PowerShell Modules

There are two different PowerShell modules:

For the purposes of this blog post, I am using the JonnMsft module.

Installing a Tesla PowerShell Module

Start installing the module by running an elevated PowerShell interface.

Install-Module tesla

Tesla with PowerShell - Untrusted Repository

If you see the Untrusted repository message, simply click on the Yes button in order to continue. Once the module is installed, close PowerShell and then reopen it.

NuGet Provider

Tesla with PowerShell - NuGet Provider

If you receive a notice about a missing or outdated NuGet provider, upgrade the NuGet provider first and then install the Tesla module.

Use this code to update the NuGet provider.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

Error Messages

Tesla with PowerShell - Error Message

If you receive an error message similar to the one above, make sure that you set SecurityProtocoltype to TLS12 before trying to install the module.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Scripts Disabled

Tesla with PowerShell - Scripts Disabled

There are a couple of ways to solve this error message, but the simplest way is to change your execution policy to the following:

Set-ExecutionPolicy -ExecutionPolicy Bypass

Tesla with PowerShell - Execution Policy Change

Click Yes when prompted.

Tesla Module

Now that you have the latest NuGet Provider and dealt with any error messages, you see that the Tesla module has three commands.

• Connect-Tesla
• Get-Tesla
• Set-Tesla

The Get and Set commands each have subcommands. I’m not going to get into any of them, so instead, if you want to learn more, here is the link in the docs to the Module where you can see examples.

Code

Below is a simple script that retrieves details about your Tesla. Notice that the first section is commented out. In order to run the script over and over again, you could embed your username and password, BUT that is not a good practice. That’s why I commented out the first section. The Connect-Tesla command prompts you for your user name and password in a secure fashion. This is great for a one-time query.

#
# $username = “tesla account”
# $password = “tesla password”
#
# $secstr = New-Object -TypeName System.Security.SecureString
# $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
# $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
# Connect-Tesla -Credential $cred
#

Connect-Tesla

$vehicles = Get-Tesla -Command vehicles
$charge_state = Get-Tesla -Command charge_state
$climate_state = Get-Tesla -Command climate_state
$drive_state = Get-Tesla -Command drive_state
$gui_settings = Get-Tesla -Command gui_settings
$vehicle_state = Get-Tesla -Command vehicle_state
$vehicles
$charge_state
$climate_state
$drive_state
$gui_settings
$vehicle_state

Results

Needless-to-say, there are some cool details that you can get from your Tesla. I am not going to go into all of them, but here are a few settings.

Tesla with PowerShell - Settings

You can even get latitude and longitude information about the car’s current location! Which, in my case, would show that my car is parked at the office. 😊

Conclusion: How to Query a Tesla with PowerShell

Why did I want to do this? Well, there is another project that I’m working on. This POC project needed to capture details from an external source. Using the results from my car was just as good an example as anything else. Since it worked, I am using this script as the external source for my other project. More to come on that POC project when it is completed. If you have any questions about how to query a Tesla with PowerShell, please feel free to touch base with me @GarthMJ or you can reach out to Recast Software here.

Back to Top