Encode and decode an URL using PowerShell coding

Instantly in 2 steps encode and decode an URL using PowerShell coding

No comments

Loading

Encode and decode an URL using PowerShell – here in this post, I will show how to encode and decode the URL using PowerShell coding, and also we will learn how to decode a SharePoint list id (GUID) using the online tool.

Introduction: Encode and decode an URL using PowerShell coding

A website’s URL, also usually known as the “website address”, is what we would enter into a web browser in order to access a specific website’s resource.

and a URL (Uniform Resource Locator) is the unique address to access any resource on the world wide web. URLs have a well-defined structure and are unique which was invented in RFC 1738 by Tim Berners-Lee, the inventor of the world wide web.

URL encoding online (URL encoding PowerShell) – What is the encoded URL?

URL encoding is a technique or mechanism for translating unprintable or special characters to a universally accepted format by web servers and browsers.

The most commonly encoded character in the URL string is the character. We see this character whenever we see a plus-sign (+) in a URL that represents the space character. The plus sign acts as a special character representing that space in a URL.

URL Decode PowerShell – What is the decoded URL?

This is just the reverse process of encoding – translating back the URL to readable English characters.

Encode and decode an URL using PowerShell Script coding

Using the below PowerShell coding we can handle the URL encoding and decoding:

cls
Add-Type -AssemblyName System.Web

$webURL="https://globalsharepoint.sharepoint.com/sites/TestSite/Shared Documents/"
Write-Host "The original url is " $webURL -ForegroundColor Green
#The below code is used to encode the URL
$urlToEncode = $webURL
$encodedURL = [System.Web.HttpUtility]::UrlEncode($urlToEncode) 
Write-Host "The encoded url is: " $encodedURL -ForegroundColor Green
#Encode URL code ends here

#The below code is used to decode the URL.
$urlTodDecode = $encodedURL
$decodedURL = [System.Web.HttpUtility]::UrlDecode($urlTodDecode)
Write-Host "The decoded url is: " $decodedURL -ForegroundColor Green
#Decode URL code ends here.

Test – Output:

URLEncodingDecoding

Using online URL encoding and decoding:

There are many open websites using which we can encode and decode the URL, the below URL is one among them.

https://urldecode.org/

Using an Online tool decodes a SharePoint List ID (GUID)

This is a great little but a very helpful tool by Eric Meyer. Input a nasty-looking URL string, and it will make it into something that you can read.

If we go to the list edit page, we will get the below nasty URL which is very unformatted and difficult to understand,

Original URL: /_layouts/listedit.aspx?List= %7BAA90DD8B%2DD2B7%2D49E9%2DA8DE%2D9138AB94CA68%7D

If we decode the above URL using this tool, we will get the below friendlier URL.

Decoded URL: /_layouts/listedit.aspx?List ={AA90DD8B-D2B7-49E9-A8DE-9138AB94CA68}

Example:

Tool URL: https://meyerweb.com/eric/tools/dencoder/

Decode SharePoint List ID (GUID) - URL Decoder/Encoder
Decode SharePoint List ID (GUID) – URL Decoder/Encoder

Source:

Summary: Encode and decode an URL using PowerShell script

Thus, in this post we have learned the below concepts:

  • How to decode a string.
  • How to encode a URL using PowerShell.
  • How to decode a URL using PowerShell.
  • How to decode URL online.
  • How to decode a SharePoint list ID (GUID) online.

References: Encode and decode an URL using PowerShell script

FAQs: Popular questions and answers about URL encoding and decoding

In this section, we will discuss the most frequently asked questions and answers related to URL encoding and decoding.

How to encode and decode URL using C# coding?

To encode and decode URLs in C#, you can use the System.Web.HttpUtility class, which provides methods to perform URL encoding and decoding operations.

Here’s an example of how to encode a URL:

string url = "https://www.example.com?param1=value1&param2=value2";
string encodedUrl = HttpUtility.UrlEncode(url);

In this example, the UrlEncode method of the HttpUtility class is used to encode the URL. The resulting encodedUrl string will contain the encoded URL.

To decode a URL, you can use the UrlDecode method of the HttpUtility class:

string decodedUrl = HttpUtility.UrlDecode(encodedUrl);

In this example, the UrlDecode method is used to decode the encodedUrl string and retrieve the original URL.

Note that URL encoding and decoding is important for transmitting data over the internet and ensuring that the data is transmitted correctly and securely.

How to encode and decode URL using Java coding?

To encode and decode URLs in Java, you can use the java.net.URLEncoder and java.net.URLDecoder classes, which provide methods to perform URL encoding and decoding operations.

Here’s an example of how to encode a URL:

import java.net.URLEncoder;

String url = "https://www.example.com?param1=value1&param2=value2";
String encodedUrl = URLEncoder.encode(url, "UTF-8");

In this example, the encode method of the URLEncoder class is used to encode the URL. The resulting encodedUrl string will contain the encoded URL.

To decode a URL, you can use the URLDecoder class:

import java.net.URLDecoder;

String decodedUrl = URLDecoder.decode(encodedUrl, "UTF-8");

In this example, the decode method of the URLDecoder class is used to decode the encodedUrl string and retrieve the original URL.

Note that URL encoding and decoding is important for transmitting data over the internet and ensuring that the data is transmitted correctly and securely.

How to encode and decode URL using Python coding?

To encode and decode URLs in Python, you can use the urllib.parse module, which provides functions to perform URL encoding and decoding operations.

Here’s an example of how to encode a URL:

import urllib.parse

url = "https://www.example.com?param1=value1&param2=value2"
encoded_url = urllib.parse.quote(url, safe='')

In this example, the quote function of the urllib.parse module is used to encode the URL. The resulting encoded_url string will contain the encoded URL.

To decode a URL, you can use the unquote function:

import urllib.parse

decoded_url = urllib.parse.unquote(encoded_url)

In this example, the unquote function of the urllib.parse module is used to decode the encoded_url string and retrieve the original URL.

Note that URL encoding and decoding is important for transmitting data over the internet and ensuring that the data is transmitted correctly and securely.

How to encode and decode URL using PHP coding?

To encode and decode a URL using PHP, you can use the built-in functions urlencode() and urldecode(), respectively.

Here’s an example of how to use these functions:

//perl
// Encoding a URL
$url = "https://www.example.com/search?q=php&lang=en";
$encoded_url = urlencode($url);
echo $encoded_url; // Output: https%3A%2F%2Fwww.example.com%2Fsearch%3Fq%3Dphp%26lang%3Den

// Decoding a URL
$decoded_url = urldecode($encoded_url);
echo $decoded_url; // Output: https://www.example.com/search?q=php&lang=en

In the above example, we first define a URL string and then encode it using urlencode(). The encoded URL is then printed to the screen. Next, we decode the encoded URL using urldecode() and print the decoded URL to the screen.

Note that when encoding a URL, characters that are not allowed in a URL, such as spaces and special characters, are replaced with a percent sign followed by two hexadecimal digits representing the ASCII value of the character. When decoding a URL, these percent-encoded characters are converted back to their original form.

How to encode and decode URL using Power Automate?

To encode and decode a URL using Power Automate, you can use the built-in expressions uriComponent() and uriComponentToString(), respectively.

Here’s an example of how to use these expressions in a Flow:

Encoding a URL:

  • Create a new step in your Flow and select “Compose” as the action.
  • In the “Inputs” section, enter the URL you want to encode as the input value.
  • In the “Expression” section, enter the following expression: uriComponent(triggerOutputs().headers.’X-Original-URL’)
  • Replace triggerOutputs().headers.’X-Original-URL’ with the actual input value you want to encode.
  • Save and run the Flow. The encoded URL will be outputted as the result of the “Compose” action.

Decoding a URL:

  • Create a new step in your Flow and select “Compose” as the action.
  • In the “Inputs” section, enter the encoded URL you want to decode as the input value.
  • In the “Expression” section, enter the following expression: uriComponentToString(‘Your encoded URL here’)
  • Replace ‘Your encoded URL here‘ with the actual input value you want to decode.
  • Save and run the Flow. The decoded URL will be outputted as the result of the “Compose” action.

Note that when encoding a URL using uriComponent(), characters that are not allowed in a URL, such as spaces and special characters, are replaced with percent-encoded values. When decoding a URL using uriComponentToString(), these percent-encoded values are converted back to their original form.

How to encode and decode URL in Power Apps?

To encode and decode a URL in Power Apps, you can use the built-in functions UrlEncode() and UrlDecode(), respectively.

Here’s an example of how to use these functions in Power Apps:

Encoding a URL:

  • Create a button or any other control to trigger the URL encoding action.
  • In the “OnSelect” property of the button or control, enter the following formula: Set(encodedURL, UrlEncode(“Your URL here”))
  • Replace “Your URL here” with the actual URL you want to encode.
  • Save and preview your app. Click on the button or control to encode the URL. The encoded URL will be stored in the encodedURL variable.

Decoding a URL:

  • Create a button or any other control to trigger the URL decoding action.
  • In the “OnSelect” property of the button or control, enter the following formula: Set(decodedURL, UrlDecode(encodedURL))
  • Replace encodedURL with the name of the variable that contains the encoded URL.
  • Save and preview your app. Click on the button or control to decode the URL. The decoded URL will be stored in the decodedURL variable.

Note that when encoding a URL using UrlEncode(), characters that are not allowed in a URL, such as spaces and special characters, are replaced with percent-encoded values. When decoding a URL using UrlDecode(), these percent-encoded values are converted back to their original form.

Why do you need to encode URLs?

You need to encode URLs to ensure that all characters in the URL are properly interpreted by the web server, web browser, or other applications that may process the URL.

URLs are used to locate and retrieve resources such as web pages, images, and other types of content from the internet. URLs may contain characters that are reserved or have special meanings in the URL syntax, such as question marks, ampersands, and spaces.

When a URL contains these special characters, it can cause problems if they are not properly encoded. For example, if a space character is included in a URL, it may be interpreted as the end of the URL and cause a “page not found” error.

Encoding URLs involves replacing special characters with percent-encoded values that represent the ASCII code for the character. This ensures that the URL is properly interpreted and processed by web servers, browsers, and other applications.

In summary, encoding URLs is necessary to ensure that URLs are correctly interpreted and processed by various applications that use them.

What is the purpose of encoded URL?

The purpose of an encoded URL is to ensure that all characters in the URL are properly interpreted by web browsers, servers, and other applications that may process the URL.

Encoded URLs are created by replacing certain characters in the URL with percent-encoded values. For example, spaces are replaced with %20, question marks with %3F, and ampersands with %26.

The purpose of this encoding is to make sure that special characters in the URL are not misinterpreted as delimiters or separators between different parts of the URL, and that they are properly recognized as part of the URL’s content.

Encoded URLs are also important for security purposes, as they can help prevent certain types of attacks such as cross-site scripting (XSS) and SQL injection by preventing malicious code from being injected into the URL.

In summary, the purpose of an encoded URL is to ensure that all characters in the URL are properly interpreted by web browsers, servers, and other applications, and to enhance the security of the URL.

What is the advantage of URL encoding?

The advantages of URL encoding include:

  • Compatibility: URL encoding ensures that URLs are compatible with different systems and platforms. URLs can be accessed from a variety of devices and applications, including web browsers, mobile devices, and web crawlers, and URL encoding ensures that they are properly interpreted by these systems.
  • Accuracy: URL encoding ensures that all characters in the URL are accurately interpreted by web servers, browsers, and other applications. This can help prevent errors, such as broken links or missing content, which can result from incorrect interpretation of characters in the URL.
  • Security: URL encoding can help improve security by preventing certain types of attacks, such as cross-site scripting (XSS) and SQL injection attacks. These attacks exploit vulnerabilities in the URL to inject malicious code into web pages or applications, but URL encoding can help prevent this by ensuring that special characters are properly encoded and cannot be exploited.
  • User-Friendliness: URL encoding can help make URLs more user-friendly by allowing them to include special characters, such as spaces or special symbols, without causing issues. Encoded URLs can be more easily shared and accessed by users, as they are less likely to encounter errors or compatibility issues.

In summary, the advantages of URL encoding include compatibility, accuracy, security, and user-friendliness. It is an important practice for ensuring that URLs are properly interpreted by different systems and applications and for improving the overall user experience.

Why is %20 used in URLs?

In URLs, %20 is used to represent a space character. This is because space characters are not allowed in URLs, as they can cause issues with interpretation and processing.

When a space character is included in a URL, it must be replaced with a valid character sequence that represents the space character. %20 is the standard encoding for a space character in a URL.

%20 is actually a hexadecimal representation of the ASCII code for a space character. In the ASCII character set, a space character is represented by the decimal value 32, or the hexadecimal value 20. When this value is percent-encoded, it becomes %20.

For example, if you want to include the phrase “hello world” in a URL, you would need to replace the space character with %20. The resulting URL would look like this:

perl
https://example.com/search?query=hello%20world

In summary, %20 is used in URLs to represent a space character, which is not allowed in URLs and must be encoded to ensure proper interpretation and processing.

Is URL encoding safe?

Yes, URL encoding is generally safe when used correctly. It is an important practice for ensuring that URLs are properly interpreted by different systems and applications and for improving the overall user experience.

However, there are some potential risks associated with URL encoding. One of the main risks is that URL encoding can make it easier for attackers to perform certain types of attacks, such as cross-site scripting (XSS) or SQL injection attacks. These attacks can exploit vulnerabilities in the URL to inject malicious code into web pages or applications.

To prevent these types of attacks, it is important to use proper security measures, such as input validation and output encoding, to prevent malicious code from being injected into URLs or web pages.

Additionally, it is important to use correct URL encoding techniques, such as percent-encoding, and to properly handle encoded URLs to ensure that they are properly interpreted by web servers, browsers, and other applications.

In summary, URL encoding is generally safe when used correctly and with proper security measures in place. It is an important practice for ensuring proper interpretation of URLs and improving user experience.

Why HTML encoding is required?

HTML encoding is required to ensure that special characters in HTML code are properly interpreted by web browsers and do not cause errors or display issues.

HTML code can include special characters, such as <, >, &, and “, which have special meanings in HTML syntax. For example, the < character is used to indicate the start of an HTML tag, and the & character is used to start an HTML entity. If these characters are used incorrectly or not properly encoded, they can cause issues with the interpretation of the HTML code by web browsers.

HTML encoding replaces these special characters with their corresponding character entity references, which are a standardized way of representing special characters in HTML code. For example, the < character is replaced with “<“, the > character is replaced with “>”, the & character is replaced with “&”, and the ” character is replaced with “””.

By using HTML encoding, web developers can ensure that their HTML code is properly interpreted by web browsers and that special characters are displayed correctly. This is especially important for user-generated content or data input by users, as it may include special characters that could cause issues with HTML syntax.

In summary, HTML encoding is required to ensure that special characters in HTML code are properly interpreted by web browsers and to prevent errors or display issues. It is an important practice for web developers to ensure that their HTML code is properly displayed and accessible to users.

See Also: PowerShell Online tutorial

You may also like the below PowerShell Online tutorials:

Looking for a PowerApps tutorial from scratch? Then you are in the right place, below are a bunch PowerApps articles you may like:

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:


Buy SharePoint Online & Office 365 Administration eBook

 

About Post Author

Do you have a better solution or question on this topic? Please leave a comment