The “Using” Statement In Powershell

When we do code in c#, we have using statement to dispose our objects. so we don’t have to. :).  What about PowerShell? Can we do that?

Here is a PowerShell function which behaves as using statement. 🙂

Function Using-Object(
$ScriptBlock = {throw "ScriptBlock is mandatory, please provide a value."})
. $ScriptBlock
if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])

So whenever we are dealing with an object that should be disposed we can use this function as below.

# $Connection object will be diposed.
UsingObject($Connection = New-Object System.Data.SQLClient.SQLConnection($ConnectionString)) {
#code goes here.

view raw


hosted with ❤ by GitHub

Isn’t that cool? 🙂

Download Blob as file in Javascript

While we are working with Javascript we may find our self in a situation where we need to let users download blob as File.

So In this post, I’ll share basic javascript function which allows us to download blob as a file in the browser.

const downloadBlobAsFile = function(data, filename){
				const contentType = 'application/octet-stream';
        if(!data) {
            console.error(' No data')

        if(!filename) filename = 'filetodonwload.txt'

        if(typeof data === "object"){
            data = JSON.stringify(data, undefined, 4)

        var blob = new Blob([data], {type: contentType}),
            e    = document.createEvent('MouseEvents'),
            a    = document.createElement('a') = filename
        a.href = window.URL.createObjectURL(blob)
        a.dataset.downloadurl =  [contentType,, a.href].join(':')
        e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
    // call the function 
    const data = "some data";
    const fileName = "filetodonwload.txt";
    downloadBlobAsFile(data, fileName);

You can find a working sample here.