Refactor powershell script