Skip to main content

How to URL Decode a String in Batch Script

URL encoding (also known as percent-encoding) is a mechanism for converting characters into a format that can be safely transmitted over the internet. You'll commonly see it in web server logs, API query strings, or any URL where special characters like spaces are replaced by a percent sign followed by two hex digits (e.g., a space becomes %20). To get the original string back, you must decode it.

Batch scripting has no native function or command to perform URL decoding. This is a complex string operation that is far beyond its simple substitution capabilities. The only reliable and effective solution is to call a more powerful scripting language that is built into Windows: PowerShell.

This guide will teach you the modern, standard, and only recommended method for URL decoding by using a PowerShell one-liner from within your batch script.

The Challenge: No Native Decoder and Why Pure Batch Fails

URL decoding is not a simple search-and-replace. It requires a script to:

  1. Scan a string for a % character.
  2. Read the next two characters.
  3. Interpret those two characters as a hexadecimal number.
  4. Look up that number in an ASCII/UTF-8 table to find the corresponding character.
  5. Replace the three-character code (%xx) with the single decoded character.
  6. Repeat this for the entire string.

Implementing this logic in pure batch script would be extraordinarily slow, complex, and would fail on many special characters. It is not a practical approach.

The Only Robust Method: Using PowerShell

The best practice is to delegate this task to PowerShell, which has powerful, built-in .NET libraries for handling web-related tasks. It can perform the decoding in a single, efficient command.

The command leverages the .NET WebUtility class: powershell -Command "[System.Net.WebUtility]::UrlDecode('Your%20Encoded%20String')"

  • powershell -Command "...": Executes the given PowerShell command from cmd.exe.
  • [System.Net.WebUtility]::UrlDecode(): This is a static method from the .NET framework that performs a standards-compliant URL decoding.

Basic Example: Decoding a Simple String

Let's decode a string that contains a space (%20), an ampersand (%26), and a question mark (%3F).

@ECHO OFF
SET "EncodedString=My%20Data%26Value%3F"

ECHO Original Encoded String: %EncodedString%
ECHO.
ECHO --- Decoding with PowerShell ---
powershell -Command "[System.Net.WebUtility]::UrlDecode('%EncodedString%')"

Output:

Original Encoded String: My%20Data%26Value%3F

--- Decoding with PowerShell ---
My Data&Value?

Storing the Decoded String in a Variable

To use the decoded string in your script, you need to capture the output from the PowerShell command. The standard way to do this is with a FOR /F loop.

@ECHO OFF
SET "EncodedString=My%20Data%26Value%3F"
SET "DecodedString="

REM The FOR /F loop executes the command and captures its output into %%V.
FOR /F "delims=" %%V IN (
'powershell -Command "[System.Net.WebUtility]::UrlDecode('%EncodedString%')"'
) DO (
SET "DecodedString=%%V"
)

ECHO The decoded string is: "%DecodedString%"
note

This is the standard, reusable pattern for calling PowerShell to get a result back into a batch variable.

Common Pitfalls and How to Solve Them

  • Special Characters in the Result: The decoded string will likely contain special characters like &, |, <, or >. If you ECHO the result variable without quoting it, these characters can break your script.

    • Solution: Always enclose the variable holding the decoded result in double quotes when you use it (e.g., ECHO "%DecodedString%").
  • PowerShell Execution Policy: In very restricted environments, the system's PowerShell execution policy might prevent the command from running.

    • Solution: You can add the -ExecutionPolicy Bypass flag to your command to ensure it runs regardless of the system policy. This is a good practice for portable scripts.
    powershell -ExecutionPolicy Bypass -Command "..."
  • Quotes within the String: If your encoded string itself contains quote characters (%22), it can break the command. PowerShell handles this fairly well, but it's a potential edge case.

Practical Example: Parsing a Web Log Entry

This script simulates reading a line from a web server log, which often contains URL-encoded request paths. The script extracts the path and decodes it to show the actual resource that was requested.

An example Log Line 192.168.1.100 - - [27/Oct/2023:10:00:00 +0000] "GET /reports/Sales%20Report%20-%20Q3.pdf HTTP/1.1"

@ECHO OFF
SETLOCAL

SET "LogLine=192.168.1.100 - - [27/Oct/2023:10:00:00 +0000] ""GET /reports/Sales%20Report%20-%20Q3.pdf HTTP/1.1"""

ECHO Original Log Line:
ECHO %LogLine%
ECHO.

REM --- Extract the encoded path (the 7th token) ---
FOR /F "tokens=7" %%P IN (%LogLine%) DO (
SET "EncodedPath=%%P"
)
ECHO Found Encoded Path: %EncodedPath%
ECHO.

REM --- Decode the path using PowerShell ---
SET "DecodedPath="
FOR /F "delims=" %%D IN (
'powershell -ExecutionPolicy Bypass -Command "[System.Net.WebUtility]::UrlDecode('%EncodedPath%')"'
) DO (
SET "DecodedPath=%%D"
)

ECHO Decoded Path: "%DecodedPath%"

ENDLOCAL

Conclusion

URL decoding is a task that highlights the limitations of pure batch scripting and demonstrates the power of leveraging modern tools like PowerShell.

  • There is no native or practical pure-batch method to URL decode a string.
  • The PowerShell [System.Net.WebUtility]::UrlDecode() method is the only recommended solution. It is fast, reliable, and built into all modern versions of Windows.
  • The standard pattern to use this in a script is to call the PowerShell command from within a FOR /F loop to capture the decoded result into a batch variable.

For any script that needs to handle URL-encoded data, this hybrid batch-and-PowerShell approach is the professional and correct way to do it.