Ported C# migration code to Powershell