PowerShell and SCCM task sequences are both great tools, but they don't always work well together. I was working with a customer who wanted to setup an automatic Windows login a the end of the initial build task sequence. The username was the same as the computer name and all accounts have the same password. The PowerShell script worked in Windows, but failed in the task sequence.
We found the issue was that Windows environment variables are not available to PowerShell, or any other scripts, in the task sequence environment. Instead, there are task sequence variables, which you can find here
Here is the PowerShell code we used to access the variables and set the login name:
#environment variable call for task sequence only
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
#set autlogin registry values
New-ItemProperty -Path "HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon" -PropertyType string -name "DefaultUserName" -Value "$pcname" -Force