ITnewbie
ITnewbie

Reputation: 520

Convert csv file to base64 format in Powershell

I have a Python script to convert the CSV file to base64.

import base64
file = open("D:\test.csv", 'rb')
base64_csv = base64.b64encode(file.read()).decode('utf-8')

I need to write a Powershell to do the same thing due to company recruitment. I tried this module but doesn't work:

$csv = Import-Csv -Path "D:\test.csv"
base64_csv = [System.Convert]::ToBase64String([char[]]$csv)

Cannot convert the "@{My CSV File content}" value of type 
"System.Management.Automation.PSCustomObject" to type "System.Char[]".

I am very new to Powershell, any help is appreciated!

Upvotes: 0

Views: 2259

Answers (1)

Anon Coward
Anon Coward

Reputation: 10832

The fact the file is CSV isn't part of the processing needed to converting it to base64 representation. You should be able to simply convert the file as raw data to a base64 string:

$base64_csv = [Convert]::ToBase64String([IO.File]::ReadAllBytes("D:\test.csv"))

Upvotes: 2

Related Questions