Monday, January 18, 2021

Move all studies of patients with ID in CSV file from PACS-A to PACS-B

OK, so here's another PowerShell DICOM script that came handy recently. So my friend, lets call him Benny, got a request from his manager to export all the studies of patients in a list provided as excel sheet and deliver them to a research group (after de-identification and everything of course) but when he called me he just needed to get them out quickly from the PACS. The list was long and there was no chance to do this one by one. So we wrote together this little PowerShell script that reads the patient id's from the excel (after saving it as csv) and use MODALIZER-SDK to do the DICOM work. here it is. I think it explains itself pretty well. We've set the destination PACS-B to be our lightweight PACS server with no plugin so the DICOM files were saved on the disk and from there he picked them up.






function MoveStudyByPatientID


$r = New-Object -ComObject rzdcx.DCXREQ


    # Lets echo to be sure

$r.Echo($my_ae_title, $pacs_a_ae_title, $pacs_a_ip_address, $pacs_a_dicom_port)

$o = New-Object -ComObject rzdcx.DCXOBJ

$e = New-Object -ComObject rzdcx.DCXELM

# Query Level - STUDY


$e.Value = "STUDY"


# Patient ID


$e.Value = $pat


# Study Instance UID


    $e.Value = ""


$i = $r.Query($my_ae_title, $pacs_a_ae_title, $pacs_a_ip_address, $pacs_a_dicom_port, "1.2.840.10008.", $o)

    if ($i.AtEnd()) {

        "No Studies for patient id: " + $pat




        while ($i.AtEnd() -ne $true)


            $res = $i.Get()

    "LOG: Study Instance UID: " + $res.GetElement(0x0020000d).Value


    $r.Move($my_ae_title, $pacs_a_ae_title, $pacs_a_ip_address, $pacs_a_dicom_port, 104, $pacs_b_ae_title, $o)





# A log never hurts

$a = New-Object -ComObject rzdcx.DCXAPP


$a.LogLevel = 5

$patients = Import-Csv -Path .\patients.csv

foreach ($pat in $patients.patient_id) 


    "LOG: Patient ID: " + $pat

MoveStudyByPatientID $pat



# That's it for now