June 6, 2020

PowerShell :: Split a long string

PowerShell ISE logo
(Last Updated On: 14th September 2016)


When I was writing some VBA for a macro malware project, I soon realised that there was a limit on the length of a string declaration in VBA which meant I had to break up a large Base64 string into multiple concatenations.  This PowerShell scripts automates that process and could be easily modified for other languages as it simply reads each character of a file, discards any formatting characters and then outputs a fixed length line to the output file.  You will need to define the $inputFilePath and $outputFilePath.

$inputFilePath = "C:\Base64.txt"
$outputFilePath = "C:\Output.txt"

$variableName = "StringVariable"
$lineSize = 200

$encodedCommand = [IO.File]::ReadAllText($inputFilePath)
$y = 0
$formattingCharacters = "`n","`r","`t"

write-output '' > $outputFilePath
while($y -lt $encodedCommand.Length)
while($x -lt $lineSize)
if ($encodedCommand[$y] -notin $formattingCharacters)
$line = "$variableName = $variableName & `""+$buffer+"`""
write-output $line >> $outputFilePath

Hope you find it useful,


Previous «
Next »

Simon is a sysadmin for a global financial organisation and specialises in Windows, security and automation.

1 Comment

  1. Pingback: an intro to macro malware – Synack

Leave a Reply

Subscribe to SYNACK via Email

%d bloggers like this: