Convert Currency To Your Own (Currency Exchange Rate)

  Sometime, your partner tells you the amount of his currency, and you want to exchange it to your own, how will you do that? Access Google, search an exchange rate website, input your amount, select the currency type, and record the result? It’s too complex. Now, I will show you the much more easier way via PowerShell.

  Create a function in your PowerShell profile, it’s the code:

function ConvertTo-MyCurrency {
    param(
        # Default: 1
        [Parameter(Mandatory=false)]         [ValidateScript({_ -gt 0})]
        Amount = 1,          # Default: USD         [Parameter(Mandatory=false)]
        [ValidateSet("CNY","AED","AFN","ALL","AMD","ANG","AOA","ARS","AUD","AWG","AZN","BAM","BBD","BDT","BGN","BHD","BIF","BMD","BND","BOB","BRL","BSD","BTC","BTN","BWP","BYN","BYR","BZD","CAD","CDF","CHF","CLF","CLP","CNH","COP","CRC","CUP","CVE","CZK","DEM","DJF","DKK","DOP","DZD","EGP","ERN","ETB","EUR","FIM","FJD","FKP","FRF","GBP","GEL","GHS","GIP","GMD","GNF","GTQ","GYD","HKD","HNL","HRK","HTG","HUF","IDR","IEP","ILS","INR","IQD","IRR","ISK","ITL","JMD","JOD","JPY","KES","KGS","KHR","KMF","KPW","KRW","KWD","KYD","KZT","LAK","LBP","LKR","LRD","LSL","LTL","LVL","LYD","MAD","MDL","MGA","MKD","MMK","MNT","MOP","MRO","MUR","MVR","MWK","MXN","MYR","MZN","NAD","NGN","NIO","NOK","NPR","NZD","OMR","PAB","PEN","PGK","PHP","PKG","PKR","PLN","PYG","QAR","RON","RSD","RUB","RWF","SAR","SBD","SCR","SDG","SEK","SGD","SHP","SKK","SLL","SOS","SRD","STD","SVC","SYP","SZL","THB","TJS","TMT","TND","TOP","TRY","TTD","TWD","TZS","UAH","UGX","USD","UYU","UZS","VEF","VND","VUV","WST","XAF","XCD","XDR","XOF","XPF","YER","ZAR","ZMK","ZMW","ZWL")]
        [string]From="USD",          # Default: CNY         [Parameter(Mandatory=false)]
        [ValidateSet("CNY","AED","AFN","ALL","AMD","ANG","AOA","ARS","AUD","AWG","AZN","BAM","BBD","BDT","BGN","BHD","BIF","BMD","BND","BOB","BRL","BSD","BTC","BTN","BWP","BYN","BYR","BZD","CAD","CDF","CHF","CLF","CLP","CNH","COP","CRC","CUP","CVE","CZK","DEM","DJF","DKK","DOP","DZD","EGP","ERN","ETB","EUR","FIM","FJD","FKP","FRF","GBP","GEL","GHS","GIP","GMD","GNF","GTQ","GYD","HKD","HNL","HRK","HTG","HUF","IDR","IEP","ILS","INR","IQD","IRR","ISK","ITL","JMD","JOD","JPY","KES","KGS","KHR","KMF","KPW","KRW","KWD","KYD","KZT","LAK","LBP","LKR","LRD","LSL","LTL","LVL","LYD","MAD","MDL","MGA","MKD","MMK","MNT","MOP","MRO","MUR","MVR","MWK","MXN","MYR","MZN","NAD","NGN","NIO","NOK","NPR","NZD","OMR","PAB","PEN","PGK","PHP","PKG","PKR","PLN","PYG","QAR","RON","RSD","RUB","RWF","SAR","SBD","SCR","SDG","SEK","SGD","SHP","SKK","SLL","SOS","SRD","STD","SVC","SYP","SZL","THB","TJS","TMT","TND","TOP","TRY","TTD","TWD","TZS","UAH","UGX","USD","UYU","UZS","VEF","VND","VUV","WST","XAF","XCD","XDR","XOF","XPF","YER","ZAR","ZMK","ZMW","ZWL")]
        [string]To="CNY"     )     if (From -eq To) {         Write-Warning -Message "Idiot! You can't convert From to To by yourself?!"         break     }     url = "https://finance.google.cn/finance/converter?a=Amount&from=From&to=To"     result = (Invoke-WebRequest -Uri url).content -replace "(.*\n)*.*bld>|\sTo.*(.*\n)*"
    Write-Host "`tAmount From = result To"
}

In this function, up to 170 currency types are supported, and it’s very easy to use. Let’s look at some samples.
 1. Quick converting. I’m in China, and the most frequent foreign currency for me is US dollar, so I will check the latest rate by this:

PS D:\Scripts> ConvertTo-MyCurrency
        1 USD = 6.3416 CNY

 2. Specify the currency type. It’s an example:

PS D:\Scripts> ConvertTo-MyCurrency -From KWD -To VND -Amount 1
        1 KWD = 76365.0686 VND

 3. Of cource, if you want to convert A to A, the result won’t be friendly.

PS D:\Scripts> ConvertTo-MyCurrency -From KWD -To KWD -Amount 1
WARNING: Idiot! You can't convert KWD to KWD by yourself?!

OK, enjoy it!

Leave a Reply

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