Get BitCoin Price PowerShell

Get BitCoin Price PowerShell

Hello,

This blog post is kind of a followup on a previous one where I told you I’m testing Monero Mining to found this site, now, I want to get a $ value in front of the mined Monero to evaluate if it’s worth it. Bitcoin is also quite popular those days, so it’s another way to apply PowerShell to real world example that may talk to you.

You can get the price of the BitCoin or other cryptocoin from websites like CoinMarketCap. It will display some graphs with the value over time, and other nice information.

Get BitCoin Price PowerShell

You can use an API from CoinMarketCap to query its database and return BitCoin or other cyrptocoins as PowerShell objects.

$ApiUrl = 'https://api.coinmarketcap.com/v1/ticker/'
$Name = 'bitcoin'
$RefCurency = 'USD'
(Invoke-WebRequest -Uri "$($ApiUrl)$($Name)/?convert=$RefCurency" | Select-Object -ExpandProperty Content | ConvertFrom-Json)
Get BitCoin Price PowerShell - PowerShell Example

Get BitCoin Price PowerShell – PowerShell Example

The API call is pretty easy, you can change $Name to match the cryptocoin you want to get information about. You can have all the possible value with:

(Invoke-WebRequest -Uri "$($ApiUrl)?limit=99999" | Select-Object -ExpandProperty Content | ConvertFrom-Json) | Select-Object -ExpandProperty name

As of today, there is 1278 cryptocoins in their database.

Now that we have the value in USD, we can apply a multiplier:

[double]$BitCoinPrice = (Invoke-WebRequest -Uri "$($ApiUrl)$($Name)/?convert=$RefCurency" | Select-Object -ExpandProperty Content | ConvertFrom-Json) | Select-Object -ExpandProperty price_usd
$BitCoinPrice * 3
Get BitCoin Price PowerShell - 3 BitCoin Price

Get BitCoin Price PowerShell – 3 BitCoin Price

And now we can see that 3btc worth ~19 849,98 USD$.

Note: I use [double] for the BitCoin price variable because this is not an integer, so PowerShell, by default, will treat it as a string and the result would have been “6618.446618.446618.44” without it.

Wallet Worth with PowerShell

Now that you know how to get a cryptocoin name and its worth, you can build a small reporting about your wallet’s worth easily:

$ApiUrl = 'https://api.coinmarketcap.com/v1/ticker/'
$Wallet= @'
Name;Amount
bitcoin;2.3
monero;5.14
ripple;152
'@ | ConvertFrom-Csv -Delimiter ';'
$RefCurency = 'USD' #USD,EUR

$PreContent = @'
<style>
table {
    border-collapse: collapse;
}
h2 {text-align:center}
th, td {
    padding: 8px;
    text-align: left;
    border-bottom: 1px solid #ddd;
}
tr:hover{background-color:#f5f5f5}
</style>
<h1>Crypto Coin Dashboard</h1>
'@
$PostContent = @"
<br>
Last Refresh: $(Get-Date)
"@

$CurrencyFormat = New-Object System.Globalization.CultureInfo("en-US")

$Wallet| ForEach-Object -Process {
    $CurrentValueInUSD = ((Invoke-WebRequest -Uri "$($ApiUrl)$($_.Name)/?convert=$RefCurency" | Select-Object -ExpandProperty Content | ConvertFrom-Json) | Select-Object -ExpandProperty price_usd) -as [double]
    New-Object -TypeName PSObject -Property @{
        Currency = $_.Name
        ValueInUSD = $CurrentValueInUSD.ToString('c',$CurrencyFormat)
        ValueInWallet = ($CurrentValueInUSD * $_.Amount).ToString('c',$CurrencyFormat)
        Amount = $_.Amount
    }
    $CurrentValueInUSD = $null
} | ConvertTo-Html -PreContent $PreContent -PostContent $PostContent -Title 'Crypto Coin Dashboard' | Out-File -FilePath wallet.htm
Get BitCoin Price PowerShell - Wallet Worth

Get BitCoin Price PowerShell – Wallet Worth

Get BitCoin Price PowerShell - Wallet Worth HTML

Get BitCoin Price PowerShell – Wallet Worth HTML

You just need to adjust the $Wallet here string to match your wallet content.

3 thoughts on “Get BitCoin Price PowerShell

  1. Pingback: Get Crypto Coins Wallet Worth PowerShell - It for DummiesIt for Dummies

Leave a Reply