# Converting to Base64 in Powershell

There are a variety of ways to send a file to Web end-point and encode it in the the process. For example, using `Invoke-RestMethod -InFile` (docs):

``````C:\PS> Invoke-RestMethod -Uri http://example.com `
-Method Post -ContentType 'multipart/form-data' `
-InFile c:\temp\test.txt
``````

However, if we want/need to include one or more files as part of a larger JSON payload, perhaps with other information for each file, we will need to convert the file(s) to Base64.

To do so, we’ll make use of .Net functionality, in particular the `System.Convert.ToBase64String` method and the `System.Web.Script.Serialization.JavaScriptSerializer` class (see note).

``````# define parameters
Param(
[Parameter(Mandatory=\$true, Position=0)]
[string]\$InputFile
)

\$base64String = [System.Convert]::ToBase64String(\$content)

Note: I chose `System.Web.Extensions` over the more common `Json.Net` because I didn’t want to have to download/nuget a dependency; `JavaScriptSerializer` proves sufficient for this task.