Site icon PowerShell is fun :)

Retrieve Coinmarketcap information using PowerShell

If you’re into Crypto and would like to see the latest price of your precious coin, then perhaps this is a blog post for you 😉 In this blog post, I will show you how you can use the API from Coinmarketcap to show you the latest stats in every newly started PowerShell session.

Preparation

To use the Coinmarketcap API service, you must first register a free account. You can do this by following these steps:

Note: The free account has a limit of 333 searches each day, 10.000 each month, and 30 requests per minute

How the script works

You can start the Get-CoinMarketCap.ps1 script from your saved location to import the function in your session by running:

. c:\scripts\Get-CoinMarketCap.ps1

The function is now available for you in the current session. Please save it to your profile, so it automatically starts every time you start a new session. You can follow these steps for that:

- notepad $profile
- Add ". c:\scripts\Get-CoinMarketCap.ps1" at the end of your profile
- Add Get-CoinMarketCap below that (Or Get-CoinMarketCap -Search BTC for example to only display BTC (BitCoin) stats)
- Save and quit
- Start a new PowerShell session

By default, the Get-CoinMarketCap function will only return the top 10. The output looks like this:

When using the -Search parameter, it will look like this when searching for BTC:

Note: The -search parameter will only search in the Top 100 items

The script

Below is the script for the Get-CoinMarketCap function, replace xxxxxxxx with your own API key which you received after registering your API account:

function Get-CoinMarketCap {
    param (
        [Parameter(Mandatory = $false, HelpMessage = "Enter the coin that you want to search for, i.e. BTC")][string]$Search
    )

    #Set values for API call
    $params = @{
        uri     = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
        Method  = 'Get'
        Headers = @{
            "Content-Type"      = 'Accept: application/json'
            "X-CMC_PRO_API_KEY" = ' xxxxxxxx'
        }
    }

    #retrieve data
    $data = Invoke-RestMethod @params
    if ($null -eq $Search) {
        $coins = $data.data | Sort-Object cmc_rank | Select-Object -First 10
    }
    else {
        $coins = $data.data | Where-Object Symbol -EQ $Search
        if ($null -eq $coins) {
            Write-Warning ("Specified {0} coin was not found, exiting..." -f $Search)
            return
        }
    }

    $total = foreach ($coin in $coins) {
        [PSCustomObject]@{
            "Rank"               = $coin.cmc_rank
            "Name"               = $coin.Name
            "Symbol"             = $coin.Symbol
            "Price in USD"       = $coin.quote.usd.Price
            "1 hour difference"  = "$($coin.quote.usd.percent_change_1h)%"
            "24 hour difference" = "$($coin.quote.usd.percent_change_24h)%"
            "7 day difference"   = "$($coin.quote.usd.percent_change_7d)%"
        }
    }
    return $total | Format-Table -AutoSize
}

Download the script(s) from GitHub here

Exit mobile version