Get cryptocurrencies price using PowerShell

Today’s script will not be directly connected with Microsoft stuff, but it will show you how to get cryptocurrencies price using PowerShell.

Cryptocurrencies every year become more popular and in my humble opinion will enter the mainstream very soon.
I thought it can be very handfull for cryptotraders who are interesting in PowerShell to have function which will get cryptocurrencies price.
Base on this script more experienced scripters can build script which will trade for them 🙂

How it works?

Function is very simple. It connects to coinmarketcap.com API and gather a list of all available cryptocurrencies.
In next step, script is filtering all cryptos array to search for one provided by the user in the function.
Example of usage below:

Get-CryptoPrice -CryptoName Bitcoin
Script:
Function Get-CryptoPrice {
    [CmdletBinding()]
                
    param
    ( 
    [Parameter(Position=0, Mandatory = $true, HelpMessage="Cryptocurrency full name", ValueFromPipeline = $false)] 
    $CryptoName
    ) 
    
    Try
    {
        $AllCryptosArray = (Invoke-WebRequest -uri https://api.coinmarketcap.com/v1/ticker/?limit=10000).Content | ConvertFrom-Json
    }
    Catch
    {
        $Exception = $_.Exception.Message
        Write-Error "Can not connect to coinmarketcap.com API. Following error occured: $Exception"
        break
    }

    $Crypto = $AllCryptosArray | Where-Object Name -eq $CryptoName
    
    If($Crypto.Count -eq 0)
    {
        Write-Output "There is no $CryptoName coin listed on coinmarketcap.com!"
    }
    Else
    {
        $Crypto | Format-Table -AutoSize
    }
}
Result:

I hope it will be usefull for some of you 😉
Enjoy!

2 thoughts on “Get cryptocurrencies price using PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *