$Server = 'rejk-p-sql003\sql01'
$Database = 'AfcCookedIFSv2'
$bufferSize = 8192
$fileLocation = 'C:\Users\ewpawa\test.jpg'
# Select-Statement for name & blob
# with filter.
$query = 'SELECT PhotoData FROM pom.objOrderCardOrdering WHERE OrderID = 21140721'
# Open ADO.NET Connection
$con = New-Object Data.SqlClient.SqlConnection
$con.ConnectionString = "Data Source=$Server; Integrated Security=True; Initial Catalog=$Database"
$con.Open()
# New Command and Reader
$cmd = New-Object Data.SqlClient.SqlCommand $query, $con
$reader = $cmd.ExecuteReader()
# Create a byte array for the stream.
$out = [array]::CreateInstance('Byte', $bufferSize)
# Looping through records
$reader.Read()
# New BinaryWriter
$fs = New-Object System.IO.FileStream $fileLocation, 'Create', 'Write'
$bw = New-Object System.IO.BinaryWriter $fs
$start = 0
# Read first byte stream
$received = $reader.GetBytes(0, $start, $out, 0, $bufferSize - 1)
While ($received -gt 0)
{
    $bw.Write($out, 0, $received)
    $bw.Flush()
    $start += $received
    # Read next byte stream
    $received = $reader.GetBytes(0, $start, $out, 0, $bufferSize - 1)
}
$bw.Close()
$fs.Close()
$fs.Dispose()
$reader.Close()
$cmd.Dispose()
$con.Close()