I'm not too familiar with PowerShell scripting but I believe you can write plain C# in it. If that's so then perhaps the C# helps.
Note that there are three PATH environment variables to choose from: the one given to the process, the one given to the user and the one given to the machine itself. For the demo code below, I chose the one for the process.
var whichPath = EnvironmentVariableTarget.Process;
string path = Environment.GetEnvironmentVariable("PATH", whichPath);
string [] pathEntries = path.Split(';');
if (pathEntries.Length > 1)
{
// Initialize to the necessary length, for efficiency.
var sb = new StringBuilder(capacity: path.Length);
for(int i = 1; i < pathEntries.Length; ++i)
{
sb.Append(pathEntries[i]).Append(';');
}
sb.Append(pathEntries[0]).Append(';');
Environment.SetEnvironmentVariable("PATH", sb.ToString(), target: whichPath);
}
";". After that, stop copying the chars to $buffer1 and start copying them to $buffer2 (that is, $buffer2 += $char). After the loop is done, simply set PATH to $buffer2 + $buffer1 - flen