Last active
July 14, 2024 03:41
-
-
Save pferreirafabricio/97c1c68057e406ff0f98840659973f0d to your computer and use it in GitHub Desktop.
🖼 Convert an image to a Base64 string with PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
OBS: | |
The AsByteStream parameter was introduced in Windows PowerShell 6.0 | |
If your PowerShell version (run `(Get-Host).Version` to find out) is lower than 6 then use the version for Powershell 5.1: | |
#> | |
$pathToImage = "/home/user/development/image.png" | |
# For Powershell 5.1 | |
[String]$base64 = [convert]::ToBase64String((Get-Content $pathToImage -Raw -Encoding Byte)) | |
# For Powershell 6+ | |
[String]$base64 = [convert]::ToBase64String((Get-Content $pathToImage -Raw -AsByteStream)) | |
Write-Output $base64 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pr0toc01 Thank you for pointing this out! The gist was updated!
Performance comparison
Powershell Core (6+)
Without
-Raw
flag:With
-Raw
flag:Powershell Windows
Without
-Raw
flag:With
-Raw
flag: