WebserviceX.net supports a Web Service which allows you to generate an image file of a Code 39 Barcode. You give the Web Service a SOAP request and the return is a Base64 encoded image file.

Code 39 Barcodes can contain 43 characters consisting of the letters A through Z, caps only, along with the numbers 0 through 9 and seven special characters (-, ., $, /, +, %, and space). It is widely used for inventory control among other uses and can be read by most barcode readers. Here is an example generated from this script.


In a WinForms Form I setup I setup four TextBoxes and Labels to collect the info required for the barcode. Beside the input TextBox I print a Label with help filling in that input.

Add-Type -AssemblyName System.Windows.Forms
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

# Main Form 
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = "Comic Sans MS,10"
$mainForm.Text = " Generate Code 39 Barcode Image File"
$mainForm.StartPosition = "centerscreen"
$mainForm.ForeColor = "White"
$mainForm.BackColor = "Wheat"
$mainForm.Size = "830, 380"
$mainForm.MinimumSize = "830, 380"

# Encode Text Label
$encodeTextLabel = New-Object System.Windows.Forms.Label
$encodeTextLabel.Location = "50,10"
$encodeTextLabel.ForeColor = "MediumBlue"
$encodeTextLabel.Size = "110, 22"
$encodeTextLabel.Text = "Text to Encode"

# Encode Text Help Label
$encodeTextHelpLabel = New-Object System.Windows.Forms.Label
$encodeTextHelpLabel.Location = "330,10"
$encodeTextHelpLabel.ForeColor = "DarkOrange"
$encodeTextHelpLabel.Size = "350, 22"
$encodeTextHelpLabel.Text = "Uppercase A-Z, 0-9 and - . $ / + % space"

# Encode Text TextBox
$encodeTextTextBox = New-Object System.Windows.Forms.TextBox
$encodeTextTextBox.Location = "160,10"
$encodeTextTextBox.Size = "150,20"
$encodeTextTextBox.ForeColor = "MediumBlue"
$encodeTextTextBox.BackColor = "White"
$encodeTextTextBox.Text = ""

Buttons are added – the Generate button calls the GenerateBarcode Function to make the SOAP request to the Web Service while the Exit button Closes the Form.

# Generate Button
$generateButton = New-Object System.Windows.Forms.Button 
$generateButton.Location = "700,10"
$generateButton.Size = "75,28"
$generateButton.ForeColor = "DarkBlue"
$generateButton.BackColor = "White"
$generateButton.Text = "Generate"

# Exit Button 
$exitButton = New-Object System.Windows.Forms.Button
$exitButton.Location = "700,40"
$exitButton.Size = "75,28"
$exitButton.ForeColor = "Red"
$exitButton.BackColor = "White"
$exitButton.Text = "Exit"

The GenerateBarcode Function first clears the $error variable to reset error checking. A New-WebServiceProxy Web Service proxy object is defined pointing to the Barcode 39 Web Services’ descriptor (WSDL). A SOAP Web Service call to Code39 is made.

In this case I made separate calls depending on whether the Show Text option was selected. The call would only work correctly if I passed the actual Automatic Variable $true or $false. If I set another variable to $true/$false or tried to pass 0/1 the input was not accepted. The image was sent as Base64 encoded but the WebServiceProxy Class object automatically decodes it, same as a browser would. The decoded data is then a byte stream and just needs written out to disk with Set-Content.

I define the output path from $PSCommandPath to write into the folder where the script is located. The filename is a combination of “Barcode_” plus the Date and Time to make each filename unique. The $error value is checked to make sure the write was good and an appropriate response is given. Finally the image is display in the Form and the COM object disposed of.

Function GenerateBarcode {
    $getBarCode = New-WebServiceProxy -uri "http://www.webservicex.net/barcode.asmx?WSDL"
    If ($showEncodeTextBox.Text -eq "0") {
        $returnedImage = $getBarCode.Code39( `
            $encodeTextTextBox.Text.ToUpper(), `
            $barSizeTextBox.Text, `
            $false, `
    } Else {
         $returnedImage = $getBarCode.Code39( `
            $encodeTextTextBox.Text.ToUpper(), `
            $barSizeTextBox.Text, `
            $true, `
    $outFile = $scriptPath + "\"  + "Barcode_" + (Get-Date -UFormat %Y%m%d_%H%M%S) + ".jpg"
    Set-Content -Path $outFile -Value $returnedImage -Encoding Byte
    If($error.count -gt 0) {
        $completedLabel.Text = "Error!"
        $completedLabel.ForeColor = "Red"
    Else {
        $completedLabel.Text = "Success!"
        $completedLabel.ForeColor = "Green"

The image is displayed from the file using PictureBox Class object.

Function PictureBox {
    $file = (Get-Item $outFile)
    $image = [System.Drawing.Image]::Fromfile($file)
    $pictureBox.Image = $image




Download – Code39Barcode.ps1